Android PendingIntent Extra

I am trying to send SMS messages from an Android application. I use PendingIntent, so I can use the broadcast receiver to check if it was sent normally. Since the sendTextMessage call will be made for each SMS message, I need to send some โ€œadditionalโ€ data to determine the actual SMS message, since my broadcast reception can do some work on a specific SMS message.

Here is my send code:

String SENT = "SMS_SENT"; Intent sentIntent = new Intent(SENT); sentIntent.putExtra("foo", "BAR"); PendingIntent sentPI = PendingIntent.getBroadcast(baseContext, 0, sentIntent, 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(baseContext, 0, new Intent(DELIVERED), 0); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(smsMessage.getNumber(), null, smsMessage.getText(), sentPI, deliveredPI); 

The problem is that in my BroadCast receiver it cannot read my extra "foo":

  public class SMSSentBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent intent) { String smsID = intent.getStringExtra("foo"); ...... } } 

smsID just becomes null.

My broadcast receiver is registered as follows:

 baseContext.registerReceiver(new SMSSentBroadcastReceiver(), new IntentFilter("SMS_SENT")); 

Given that the intent filter works, what am I doing wrong? Why are additional services also not sent?

Any help is appreciated. Thanks

+8
android android-intent android-service
source share
1 answer

What I suspect has happened (at least it happened to me) is that you use the SMS_SENT intent somewhere else, while at the same time this intent does not have โ€œfooโ€ in the extra functions. Calling PendingIntent.getBroadcast with the same intent and request code will return your original pending intent.

If this actually means the same intention, you need to use something like PendingIntent.FLAG_UPDATE_CURRENT to update the intention with new additional features. If this does not mean that it is the same intention, it must have a different request code so that it does not correspond to another intention (that is why working with a unique request code worked).

+14
source share

All Articles