android - 捕捉短信发送和传递事件

标签 android sms broadcastreceiver smsmanager

我正在从我的应用发送短信。我能够在有服务提供商的设备上成功地做到这一点,但在一些服务提供商不可用的平板电脑设备和模拟器上,我想显示相应的错误消息。我正在使用以下代码(从 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/

相关文章:

android - 改造返回空响应体

java - 如何更改各种微调器值的 EditText 字段

android - gps打开/关闭时如何触发广播接收器?

iphone - 如何从 iPhone 应用程序访问和打开 iPhone 的 SMS API

java - 尽管设备已收到消息,但未从 SMS 检索器 API 接收 SMS 内容

android - 带有广播接收器的工作管理器在应用程序关闭时不起作用

没有取消注册的Android广播接收器抛出运行时错误

android - 从 Web 开发到 Android 开发

android - 关于Room和Paging Library的一些问题

Android 4.4 SMS API - 无法更改平板电脑中的默认 SMS 应用程序