4

In thread How to monitor each of Sent SMS status? it is described how you can monitor the status of a sent/delivered SMS via broadcast.

However, I haven't found: how do you identify to which SMS the broadcast belongs? There doesn't seem to be any information in getResultData() nor getResultExtras() as far as I have checked.

My use case is: I send multiple SMS in a loop one right after another. I want to monitor the status/success of the deliver for each message. How do I know which broadcast belongs to which message. (Delaying the delivery of each sms until I've gotten a broadcast for each previous one isn't really an option).

Community
  • 1
  • 1
Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192

1 Answers1

5

Mathias,

Referring to the code in the question you linked. When you create the Intent to be launched by the PendingIntent instead of just giving it an Action String you can add an extra to it to help determine which SMS it belongs to...

Intent sentIntent = new Intent(SENT);
sentIntent.putExtra("smsNumber", someValue);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent, FLAG_UPDATE_CURRENT);

Intent deliveredIntent = new Intent(DELIVERED):
deliveredIntent.putExtra("smsNumber", someValue);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, deliveredIntent, FLAG_UPDATE_CURRENT);

This way you should be able to retrieve the "smsNumber" value inside the BroadcastReceiver

Hope this helps!

Edit by Mathias Lin: Important that you pass the flag FLAG_UPDATE_CURRENT with the pending intent, so that the extras get passed along and actually arrive with the broadcast.

Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
Will Tate
  • 33,439
  • 9
  • 77
  • 71
  • Thanks for that tip, it does make sense. However, I tried it, but when I read out the relevant extra from the intent (second parameter in onReceive of the broadcast receiver), it's null. In fact, the intent.getExtras().size() is still 0. Seems that the extras are not passed along all the way to through the pending intent and broadcast. – Mathias Conradt May 08 '11 at 03:19
  • 2
    Note: you need to pass FLAG_UPDATE_CURRENT to the pending intent, in order for the extras to be passed along. Otherwise, the extras won't arrive with the broadcast. – Mathias Conradt May 08 '11 at 05:26
  • @Mathias Lin If you put FLAG_UPDATE_CURRENT you will vanish all you extras in previous Intents. And if one of messages will be delivered only the last sms will be pointed as delivered (even if it isn't) – Maxim Jul 29 '11 at 11:49
  • The solution that worked for me (not so elegant thought) was to use different Action strings for each message. – Maxim Jul 29 '11 at 12:00
  • 3
    @William Have you tried sending multiple SMS.. I am facing a problem in receiving broadcast receiver for multiple sms. I am receiving same number for all SMS.. Is their any solutions for that..Much appreciated – vinothp Aug 23 '12 at 16:04
  • how do you later identify if that message has been delivered or not after this broadcast? maybe when you are coming second time? – Cyph3rCod3r Aug 02 '18 at 10:04