Differentiating delivery reports of two separate SMS's

后端 未结 1 1780
臣服心动
臣服心动 2021-01-13 03:39

In a service, when sending smses in a loop with a broadcast receiver listening for delivery reports, how to differentiate the delivery report of each sms sent? This is simil

相关标签:
1条回答
  • 2021-01-13 03:53

    You add the message number extra to the Intent like this:

    delivered.putExtra("MsgNum", serialnum);
    

    and you try to extract it like this:

    USS.execute(intent.getStringExtra("Msgnum"));
    

    In putExtra() you have uppercase "N", in getStringExtra() you use lowercase "n".

    This is why you should always use constants for stuff like this. It prevents you spending hours trying to find errors caused by typographical errors.

    Try this:

    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 PendingIntents based on OP's comment

    OP wrote in a comment:

    My bad for the typo, felt like a sheep due to that, i tested it, it does not give a null value now, instead, it gives me the serial number of the first message sent in the loop for all messages, if i send 17 20 24 21 25 27, it gives me only 17 for all the delivery reports

    Your problem is the way PendingIntent works. The system manages a pool of PendingIntents. 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 causes the system to search for a PendingIntent that matches the parameters you've passed in (in this case, your Intent). However, the matching algorithm that PendingIntent uses only compares certain fields of the Intent to determine if it is the one that you are looking for. In particular, it does not compare extras. So this means after you've created the first PendingIntent, the call to PendingIntent.getBroadcast() will always return the same PendingIntent from the pool (and not create a new one, which is what you want).

    In order to make the call to PendingIntent.getBroadcast() create a new PendingIntent every time you call it, try making the parameters you pass to the call unique (for example: by making the ACTION in the Intent unique). Also, since each of these PendingIntents will only be used once you should set the FLAG_ONE_SHOT when obtaining the PendingIntent like this:

    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 the ACTION will be different for each call to PendingIntent.getBroadcast(), this should solve your problem.

    EDIT2: Add alternative method of registering broadcast receivers based on discussion in comments

    If you create a class that extends BroadcastReceiver, you can add that to the manifest and then you don't need to explicitly register the broadcast receiver at all. Something like this:

    public class MessageStatusReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // This is called when the status of your SMS changes (delivery or send status)
            // .. put your code here ..
       }
    }
    

    Declare the receiver in your manifest:

    <receiver android:name=".MessageStatusReceiver" />
    

    In your code that sends the SMS, do this:

    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);
    
    0 讨论(0)
提交回复
热议问题