I am Developing an App , In which I send SMS and I need to find if It's delivered or not. Every thing seems to be well if I send A message to a person.
But In some situations I got wrong Information , for example , First I send A message to number 0000 (But It will not deliver) and after it I send a message to number 0001 and It delivers (message to 0000 is still not delivered) but I got a toast with : sms delivered to 0000(But only message to 0001 is delivered) , What Should I do to fix this conflict in Delivery reports ?
Here is my code:
try {
SmsManager smsManager = SmsManager.getDefault();
String to = "5556";
String body = "Test Message";
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED).putExtra("senderNumber", to), 0);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(arg0, "SMS sent", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(arg0, "Error", Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
String s = arg1.getStringExtra("senderNumber");
Toast.makeText(getBaseContext(), "SMS delivered to " + s, Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(DELIVERED));
smsManager.sendTextMessage(to.getText().toString(), null, body.getText().toString(), sentPI, deliveredPI);
}
catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
Update:
I find that If I add a unique id to Intent action (DELIVERED+id) It will be fine and no conflict. but I don't register the Receiver on creating message , I use manifest file to do this:
<receiver android:name=".SendBroadcastReceiver" >
<intent-filter>
<action android:name="com.example.myapp.SMS_SENT" />
<action android:name="com.example.myapp.SMS_DELIVERED" />
</intent-filter>
</receiver>
And also a Receiver class named SendBroadcastReceiver to handle sms delivery.
If I add a unique id to action , how can I add them in manifest file?