You add an additional message number to the intent as follows:
delivered.putExtra("MsgNum", serialnum);
and you try to extract it like this:
USS.execute(intent.getStringExtra("Msgnum"));
In putExtra() you have upper case "N" , in getStringExtra() you use lowercase "n" .
That is why you should always use constants for such things. This prevents you from spending hours trying to find errors caused by typographical errors.
Try the following:
public static final String EXTRA_MSGNUM = "MsgNum";
then use:
delivered.putExtra(EXTRA_MSGNUM, serialnum);
and
USS.execute(intent.getStringExtra(EXTRA_MSGNUM));
EDIT: add something about generating different PendingIntent based on OP comment
OP wrote in a comment:
My bad typo, because of this I felt like a sheep, I tested it, it now does not give a zero value, instead it gives me the serial number of the first message sent in a loop for all messages, if I send 17 20 24 21 25 27 this gives me only 17 for all delivery reports
Your problem is how PendingIntent works. The system manages the PendingIntent s pool. When your code does:
String DELIVERED = "SMS_DELIVERED"; Intent delivered = new Intent(DELIVERED); delivered.putExtra("MsgNum", serialnum); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, Integer.parseInt(serialnum), delivered, 0);
This forces the system to look for a PendingIntent that matches the parameters you passed (in this case, your Intent ). However, the matching algorithm that PendingIntent only uses compares specific Intent fields to determine if it is the one you are looking for. In particular, it does not compare additional features . So this means that after creating the first PendingIntent calling PendingIntent.getBroadcast() will always return the same PendingIntent from the pool (rather than creating a new one that you want).
To make the call PendingIntent.getBroadcast() create a new PendingIntent every time you call it, try to make the parameters that you pass to the unique call (for example: making ACTION in Intent unique), in addition, since each of these PendingIntent will be used only after setting FLAG_ONE_SHOT when receiving a PendingIntent as follows:
String DELIVERED = "SMS_DELIVERED" + serialnum; // Unique ACTION every time Intent delivered = new Intent(DELIVERED); delivered.putExtra("MsgNum", serialnum); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, Integer.parseInt(serialnum), delivered, PendingIntent.FLAG_ONE_SHOT);
Since ACTION will be different for every call to PendingIntent.getBroadcast() , this should solve your problem.
EDIT2: add an alternative method for registering broadcast receivers based on discussion in comments
If you create a class that extends BroadcastReceiver, you can add this to the manifest, and then you do not need to explicitly register the broadcast receiver at all. Something like that:
public class MessageStatusReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {
Declare the recipient in the manifest:
<receiver android:name=".MessageStatusReceiver" />
In your code that sends SMS, do the following:
String DELIVERED = "SMS_DELIVERED" + serialnum; // Unique ACTION every time Intent delivered = new Intent(context, MessageStatusReceiver.class); delivered.setAction(DELIVERED ); // Set action to ensure unique PendingIntent delivered.putExtra("MsgNum", serialnum); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, Integer.parseInt(serialnum), delivered, PendingIntent.FLAG_ONE_SHOT);