I am currently trying to get the confirmation for each sended SMS. I need to be sure that my SMS are send, so I used a BroadCastReceived to get the information :
Intent sentIntent = new Intent(SMS_SEND);
sentIntent.putExtra("key", idSms);
PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
SmsManager manager = SmsManager.getDefault();
try{
manager.sendTextMessage(exp, null, sms, sentPI, null);
put("sending " + sms); //Just a method to print in a textview use has a console
} catch (IllegalArgumentException e){
put("Exception " + e.getMessage());
}
and use a broadcast receiver like this
public void onReceive(Context context, Intent intent){
String idsms = intent.getExtras().getString("key");
switch (getResultCode()) {
case Activity.RESULT_OK:
put("ACK : #" + idsms);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_RADIO_OFF:
case SmsManager.RESULT_ERROR_NULL_PDU:
case SmsManager.RESULT_ERROR_NO_SERVICE:
put("BOOM " + getResultCode() + "\n\tfrom sms #" + idsms);
break;
}
}
This work like a charm until I try to send multiple messages at the same time, the extra receive is always from the last SMS send, so I can't ID which text are send and which are not.
Here is a simple example of what will happen.
When I use a loop to send 3sms:
id : 1, message : SMS 1
id : 2, message : SMS 2
id : 3, message : SMS 3
And the received will get:
ACK : #3
ACK : #3
ACK : #3
I understand that this come from the PendingIntent.FLAG_UPDATE_CURRENT but I can't find a solution. Anyone can explain to me how I should use the PendingIntent.getBroadcast(..) to be able to manage this or at least to put me on the right track.
Your problem is due the fact that PendingIntents can be reused by the system, if certain things about the requests are not different. In your code, you're passing FLAG_UPDATE_CURRENT, which is causing the stored Intent and its extras to be updated each time a PendingIntent is requested. This is why you're getting id : 3 for each of the messages. To correct this, you can call getBroadcast() with a unique request code (the second parameter) each time, which will create a new PendingIntent for each request, each with a separate Intent with their own extras.
In your case, the fix should be simple, assuming that idSms is unique for each message.
PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(),
Integer.parseInt(idSms),
sentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
来源:https://stackoverflow.com/questions/28586451/trying-to-id-the-sms-delivery-confirmation