我正在从我的应用程序发送短信。我能够在具有服务提供商的设备上成功地做到这一点,但在某些平板电脑设备和服务提供商不可用的模拟器上,我想显示相应的错误消息。我为此使用以下代码(来自此链接):
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);
但是任何一个BroadcastReceiver
都不会在没有服务提供商的设备上执行。我期待两者都BroadcastReceiver
被执行并返回负面回应。
请注意,我没有添加任何与BroadcastReceiver
in相关的内容AndroidManifest.xml
。虽然我添加了短信权限。
难道我做错了什么?任何帮助表示赞赏。