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.