Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You add the message number extra to the Intent like this:</p> <pre><code>delivered.putExtra("MsgNum", serialnum); </code></pre> <p>and you try to extract it like this:</p> <pre><code>USS.execute(intent.getStringExtra("Msgnum")); </code></pre> <p>In <code>putExtra()</code> you have <strong>uppercase "N"</strong>, in <code>getStringExtra()</code> you use <strong>lowercase "n"</strong>.</p> <p>This is why you should <strong>always</strong> use constants for stuff like this. It prevents you spending hours trying to find errors caused by typographical errors.</p> <p>Try this:</p> <pre><code>public static final String EXTRA_MSGNUM = "MsgNum"; </code></pre> <p>then use:</p> <pre><code>delivered.putExtra(EXTRA_MSGNUM, serialnum); </code></pre> <p>and:</p> <pre><code>USS.execute(intent.getStringExtra(EXTRA_MSGNUM)); </code></pre> <p><strong>EDIT: Add something about generating different <code>PendingIntent</code>s based on OP's comment</strong></p> <p>OP wrote in a comment:</p> <blockquote> <p>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</p> </blockquote> <p>Your problem is the way <code>PendingIntent</code> works. The system manages a pool of <code>PendingIntent</code>s. When your code does:</p> <pre><code>String DELIVERED = "SMS_DELIVERED"; Intent delivered = new Intent(DELIVERED); delivered.putExtra("MsgNum", serialnum); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, Integer.parseInt(serialnum), delivered, 0); </code></pre> <p>This causes the system to search for a <code>PendingIntent</code> that matches the parameters you've passed in (in this case, your <code>Intent</code>). However, the matching algorithm that <code>PendingIntent</code> uses only compares certain fields of the <code>Intent</code> to determine if it is the one that you are looking for. In particular, <strong>it does not compare extras</strong>. So this means after you've created the first <code>PendingIntent</code>, the call to <code>PendingIntent.getBroadcast()</code> will always return the same <code>PendingIntent</code> from the pool (and not create a new one, which is what you want).</p> <p>In order to make the call to <code>PendingIntent.getBroadcast()</code> create a new <code>PendingIntent</code> every time you call it, try making the parameters you pass to the call unique (for example: by making the ACTION in the <code>Intent</code> unique). Also, since each of these <code>PendingIntent</code>s will only be used once you should set the FLAG_ONE_SHOT when obtaining the <code>PendingIntent</code> like this:</p> <pre><code>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); </code></pre> <p>Since the ACTION will be different for each call to <code>PendingIntent.getBroadcast()</code>, this should solve your problem.</p> <p><strong>EDIT2: Add alternative method of registering broadcast receivers based on discussion in comments</strong></p> <p>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:</p> <pre><code>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 .. } } </code></pre> <p>Declare the receiver in your manifest:</p> <pre><code>&lt;receiver android:name=".MessageStatusReceiver" /&gt; </code></pre> <p>In your code that sends the SMS, do this:</p> <pre><code>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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload