1

我正在从我的应用程序发送短信。我能够在具有服务提供商的设备上成功地做到这一点,但在某些平板电脑设备和服务提供商不可用的模拟器上,我想显示相应的错误消息。我为此使用以下代码(来自此链接):

            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被执行并返回负面回应。

请注意,我没有添加任何与BroadcastReceiverin相关的内容AndroidManifest.xml。虽然我添加了短信权限。

难道我做错了什么?任何帮助表示赞赏。

4

1 回答 1

1

rowItem 是我的列表视图的持有者类。我正在调用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");
            }
        }
于 2013-11-06T13:13:34.607 回答