I want to send sms to multiple people and verify whether sms sent or not. I checked multiple links (mentioned here) and got the idea of using PendingIntent
and broadCast Receiver
for confirmation.
Practical way to find out if SMS has been sent
Sending text messages programmatically in android
http://mobiforge.com/design-development/sms-messaging-android
But the key problem is that, I have different 50 contacts number in an arrayList
and their different msgs in another arrayList
.
I use this code :
for (Condition) {
sms = SmsManager.getDefault();
try {
. . . sms.sendTextMessage(phoneNumbers[i], null, messages[i], sentPI, deliveredPI);
}
catch(IllegalArgumentException e) { }
}
Now, I can't identify how many people do get their msg and how many don't. Because as shown in post(mentioned link above), every time we just get one msg, "SMS delivered".
So please let me know, how can I put "extras" in Intent
, when I send msg and get the extras from broadcast Receiver
to get the detail of specific contact/person.
One More thing : There are four different option for flag value in PendingIntent
(FLAG_ONE_SHOT
, FLAG_NO_CREATE
, FLAG_CANCEL_CURRENT
,FLAG_UPDATE_CURRENT
).
Which one should I use when I send messages in for loop for correct result?
Best Answer
This is a very simple example to demonstrate the use of the send and delivery
PendingIntent
s available for all of theSmsManager#send*()
methods, and attaching data to those to easily differentiate the results in the Receiver.Attaching that data is as simple as putting extras on the
Intent
s backing thePendingIntent
s we pass to thesend*()
methods. The catch is thatPendingIntent
s might not behave as one expects. To conserve resources, the system will only create new ones when it must. Theget*()
methods will only return a distinctPendingIntent
if theIntent
is different per theIntent#filterEquals()
method, the request code is not currently in use for an equalIntent
, or an appropriate flag is passed.Different extras on an otherwise-same
Intent
with the same request code will not cause a newPendingIntent
to be created. Depending on the flag passed in that case, those extras might be ignored, or overwrite those in a currently activePendingIntent
, which can lead to incorrect results.In our example, we're basically using the same
Intent
for each send, so we'll ensure a distinctPendingIntent
for each by passing unique request codes. This simple example uses the size of a shrinking list for those codes, which will be unique in the context of a single run. The request code can ultimately be any arbitraryint
, as long as you know it's unused at the time of request.The system will want to cache these
PendingIntent
s, should we need them again in the near future, so we'll also passFLAG_ONE_SHOT
to "clear them out" after use, and make sure we get the correct, current extras in subsequent runs.Notes:
Do make note of the method we're using to get the delivery status. The result code in the Receiver is not a reliable indicator. We must check the
getStatus()
return of theSmsMessage
obtained from the PDU extra on theIntent
to get the actual result.Also be aware that not all carriers provide delivery results, in which case the delivery
PendingIntent
s will never fire. Do not rely on a delivery result.This example uses a "correct", albeit simple, method to sequentially send multiple messages, in that it waits until the current send is compete before proceeding to the next. For short lists, you might be able to get away with a loop firing all of the sends as quickly as it executes, but this can result in a generic failure if the system can't keep up.
As noted, this is a very simple example. It is not really suitable for production, as the dynamically registered Receiver is tied to the
Activity
's lifecycle. Ideally, you'd want to implement a static Receiver class, registered in the manifest, and use explicitIntent
s to target it. Using aService
to process the results is also recommended, and those results could be delivered to the UI through any number of mechanisms; e.g.,LocalBroadcastManager
, another event bus implementation,Intent
s,Notification
s, etc.