我正在从我的应用发送短信。我能够在有服务提供商的设备上成功地做到这一点,但在一些服务提供商不可用的平板电脑设备和模拟器上,我想显示相应的错误消息。我正在使用以下代码(从 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);
但是任何 BroadcastReceiver
都不会在没有服务提供商的设备上执行。我期望 BroadcastReceiver
都得到执行并返回否定响应。
请注意,我在AndroidManifest.xml
中未添加任何与BroadcastReceiver
相关的内容。虽然我已经添加了短信权限。
我做错了什么吗?任何帮助表示赞赏。
最佳答案
rowItem 是我的 ListView 的 holder 类。我在特定项目上调用 onItemSelectedListener
,因此对于您的平板电脑或任何其他设备,我的代码有效。如果不支持 SMS,则显示 Toast。
if(rowItems.get(position).getTitle().endsWith("SMS"))
{
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("address", "0"+9999999999);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
}
else {
toast("No SMS Support");
}
}
关于android - 捕捉短信发送和传递事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19812628/