开发者

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 ev开发者_运维百科ery 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);        
}


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);


The simple this is that use a unique id for each deliveredPI, because if you do not then android considers all the same and for all the sms's the deliveredPi will show the result of any one( do not know if first or last).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜