Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting multiple broadcasts from intents?

http://mobiforge.com/developing/story/sms-messaging-android

I used the example code in the above link my own application for sending an SMS, but I run into a problem when checking the sent status of my message. What happens is, the toast message will pop up for every message I have attempted to send. So basically, let's say I've already sent 3 messages. When I go to send my 4th message, the toast message will pop up 4 times. It seems that perhaps the BroadcastReceiver is receiving the same broadcast from every intent used so far? I cannot figure out exactly why this is happening, or how to stop it. Any help or insights will be greatly appreciated!

Here is the specific method that causes this:

 //---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{        
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

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

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
        new Intent(DELIVERED), 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));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
}
like image 846
John Avatar asked May 30 '11 19:05

John


1 Answers

First, you have a new BroadcastReceiver(){ every time you call sendSMS, so they pile up, one more each time you call sendSMS. You could add unregister(this); at the bottom of each BroadcastReceiver but better would be to move the Broadcast receiver out of this function. You can create+register one in onResume() and unregister it in onPause().

Second, read this link

if you ever want to send data along with your pendingIntent you need to help Android distinguish your pending intents better ...
e.g.

 PendingIntent deliveredPI = PendingIntent.getBroadcast(this, uniqueIdPerSMS++,
    new Intent(DELIVERED), PendingIntent.FLAG_CANCEL_CURRENT);
like image 121
Robert Avatar answered Oct 14 '22 05:10

Robert