I'm sending SMS from my app. I'm able to do that successfully on devices with service providers but on some tablet devices and emulator where service provider is not available, I want to display corresponding error message.I'm using following code for that (referred from this link):
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(
ClipResultActivity.this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(
ClipResultActivity.this, 0, new Intent(DELIVERED),
0);
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
if (getResultCode() == Activity.RESULT_OK)
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(),
"SMS not sent", Toast.LENGTH_SHORT)
.show();
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
if (getResultCode() == Activity.RESULT_OK)
Toast.makeText(getBaseContext(),
"SMS delivered", Toast.LENGTH_SHORT)
.show();
else
Toast.makeText(getBaseContext(),
"SMS not delivered", Toast.LENGTH_SHORT)
.show();
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(PHONE_NO, null, SMS_BODY, sentPI,
deliveredPI);
But any of the BroadcastReceiver
is not getting executed on devices with no service provider. I was expecting both BroadcastReceiver
to get executed and return negative responses.
Please note that I have NOT ADDED anything related to BroadcastReceiver
in AndroidManifest.xml
. Though I have added SMS permissions.
Am I doing something wrong? Any help appreciated.