0

I am writing a program that sends SMS message in Android. After a send button clicked, I want to get the status of the message like Sent, and delivered. This part works fine. The problem is I want to get the telephone no to which the message is sent to and delivered to inside broadcast. This is necessary because the SMS could be sent to many users at once. How can I do that? I tried to put the no on Intent and get it inside broadcast receiver, but it is not working. Here is sample code:

 public void sendSMS(final String message, final String tele) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    SMSStatus sentStatus = new SMSStatus();
    SMSDelivery deliveryStatus = new SMSDelivery();
    //broadcast these during send and delivery
    PendingIntent sentPI = PendingIntent.getBroadcast(SMS.this, 0, new Intent(SENT).putExtra("telephone", tele), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(SMS.this, 0, new Intent(DELIVERED).putExtra("telephone",tele), 0);

    registerReceiver(sentStatus, new IntentFilter(SENT));
    registerReceiver(deliveryStatus, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(tele, null, message, sentPI, deliveredPI);
}

And one of the broadcast receiver:

class SMSStatus extends BroadcastReceiver {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            //Toast.makeText(SMS.this, "Sent to " + arg1.getExtras().getString("telephone"), Toast.LENGTH_LONG).show();
            String no = arg1.getExtras().getString("telephone");
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "Message sent " + no, Toast.LENGTH_SHORT).show();
                    break; 
                ...
           }
}

Just for information, the first code is called inside a loop passing different telephone no.

birraa
  • 430
  • 1
  • 4
  • 15
  • what is the problem, is it null or toast is not displaying ? – varunkr May 04 '16 at 08:33
  • @varunkr it displays but always only the first tel. no. It does not get the second or third tel. no. – birraa May 04 '16 at 08:35
  • then please post the loop part as well – varunkr May 04 '16 at 08:38
  • 1
    Try adding FLAG_UPDATE_CURRENT flag to your pending intent like this: `PendingIntent sentPI = PendingIntent.getBroadcast( getApplicationContext(), 0, new Intent(SENT).putExtra("telephone", tele), PendingIntent.FLAG_UPDATE_CURRENT);` – ishmaelMakitla May 04 '16 at 08:42
  • Do you think it is necessary? I don't think the problem is on the loop. – birraa May 04 '16 at 08:43
  • @ishmaelMakitla I was thinking that every time the loop calls the function, new PendingIntent is created. So does changing to PendingIntent.FLAG_UPDATE_CURRENT make a difference? – birraa May 04 '16 at 08:50
  • I suggested it since the only thing that really changes is the "extra's data" - I thought this might have the desired effect - you can still try it out and see how the broadcast are delivered - furthermore, I don't think looping to create essentially the same pending intent will have the effect you desire - you should create distinct PendingIntent for each iteration. – ishmaelMakitla May 04 '16 at 08:56
  • @ishmaelMakitla That was the problem. It is working now. Thanks all for the help. – birraa May 04 '16 at 09:31
  • I am glad this helped - just to be sure, please check the answer and confirm if these are the changes you made - you can also accept the answer so that your question is marked as answered. By the way you can read a bit more about FLAG_UPDATE_CURRENT and other PentindIntent flags [here](http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT) – ishmaelMakitla May 04 '16 at 09:40

1 Answers1

1

Change your sendSMS code into:

public void sendSMS(final String message, final String tele) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    SMSStatus sentStatus = new SMSStatus();
    SMSDelivery deliveryStatus = new SMSDelivery();
    //broadcast these during send and delivery
   PendingIntent sentPI = PendingIntent.getBroadcast(SMS.this, 0, new Intent(SENT).putExtra("telephone", tele), PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(SMS.this, 0, new Intent(DELIVERED).putExtra("telephone",tele), PendingIntent.FLAG_UPDATE_CURRENT);

    registerReceiver(sentStatus, new IntentFilter(SENT));
    registerReceiver(deliveryStatus, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(tele, null, message, sentPI, deliveredPI);
}
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32