Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:34:52+00:00 2026-05-25T21:34:52+00:00

I have a program that sends pre-defined text messages to a group of people

  • 0

I have a program that sends pre-defined text messages to a group of people at a push of a button. I have it working well but the problem I have is that when it sends the messages, it pops up with 2 toasts per message sent. Code:

package com.mfd.alerter;

//imports

public class homeScreen extends Activity {

//buttons

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //vars

    // Grab the time
    final Date anotherCurDate = new Date();  
    SimpleDateFormat formatter = new SimpleDateFormat("km");  
    final String formattedTime = formatter.format(anotherCurDate);

    // Contacts
    final String[] numbers = getResources().getStringArray(R.array.numbers);

    // Start messages. Only 1 is given to shorten post
    callStructureFire.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String msgText = "MFD PAGE OUT:\nStructure Fire\nTimeout:"+formattedTime;
            for (int i = 0; i < numbers.length; i++) {
                sendSMS(numbers[i], msgText);
            }
        }
    });

    //more call types. not important.
}

//---sends a SMS message to another device---
private void sendSMS(String numbers, String message)
{           
    String SENT = "SMS_SENT";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT), 0);


    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(numbers, null, message, sentPI, null);               
}  

//action bar stuff. not important.

}

More in detail: Lets say I send the text to 3 people, 6 toast messages will pop up saying “SMS Sent”. How do I make it so only 3 will show up?

Also, Is there a way to maybe add a counter of the messages sent? Ex: “Message 1/10 sent”, “Message 2/10 sent”, etc?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:34:52+00:00Added an answer on May 25, 2026 at 9:34 pm

    Aren’t you supposed to register the receiver only once and not every time you call sendSMS.
    You get 6 Toasts with three sms messages because you have 3 BroadCastReceivers. So in the first run you get 1 Toast. In the second run you get 2 Toasts (the receiver that was registered in the first run is called, and the one in the second). In the third run all three receivers are called, so you get three more toasts. All in sum – 6 Toasts…

    so I guess, you have to register only one receiver before the for loop where you call sendSMS, or if you want the registration in sendSMS, then you have to unregister at the end of the method.

    I hope this helps,
    cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started work on asmall program that sends text over net (TON)
I have a program that sends MSMQ messages to a remote machine. This works
I have been trying to set up a program that sends a push notification
I have a Java program that connects to a webserver using SSL/TLS, and sends
I have a program that can send text to any other program for further
I have program that requires Python 3, but I develop Django and it uses
We have a thread program that sends bulk mail. The information like 1. To
I'm developing a program that sends tweets. I have this piece of code: StringBuilder
I maintain a program that sends messages between users. The clients connect to a
I've coded a program in C that sends messages to the stdout using printf

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.