質問

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.

役に立ちましたか?

解決

rowItem is my holder class for my listview. I am calling onItemSelectedListener on specific item, so for your tablet or any other device, my code works. If there is no support for SMS, then it show 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");
            }
        }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top