android – SMS delivery report for SMS_DELIVERED Intent not working-ThrowExceptions

Exception or error:

I have gone through several solutions provided in Stackoverflow.com for getting delivery reports for SMS on android device programmatically. I have implemented (borrowed from one of the contributors on Stackoverflow.com) 2 Broadcast Receivers, one for PendingIntent = “SMS_SENT” and another for PendingIntent=”SMS_DELIVERED”. Upon running the app, the Text message is sent and I get the “SMS SENT” from sentReceiver but I do not get any response from the deliverReceiver. The same program apparently works for the contributors on Stackoverflow.com. My device is Samsung Galaxy S5, 6.0.1 os. I have tried code from several posting but all of them behave similarly. On the device there is no option to turn the delivery report on/off either. Appreciate some guidance. Here is the code:

     public class DeliverReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent arg1) {

            System.out.println("+++ DeliverReceiver resultcode = "+getResultCode());
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS Delivered", LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    makeText(context, "SMS Not Delivered",
                            LENGTH_SHORT).show();
                    break;
            }

        }
    }





public class SentReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent arg1) {

            System.out.println("+++ SentReceiver resultcode = "+getResultCode());
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    makeText(context, "+++sms_sent+++", LENGTH_SHORT).show();
                    //startActivity(new Intent(SendSMS.this, ChooseOption.class));
                    //overridePendingTransition(R.anim.animation, R.anim.animation2);
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    makeText(context, "Generic failure", LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    makeText(context, "No service", LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    makeText(context, "Null PDU", LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    makeText(context, "Radio off", LENGTH_SHORT).show();
                    break;
            }

        }
    }

    public class MainActivity extends AppCompatActivity {
        private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 101;

        private BroadcastReceiver sendBroadcastReceiver = new SentReceiver();
        private BroadcastReceiver deliveryBroadcastReceiver = new DeliverReceiver();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission
                        .SEND_SMS}, MY_PERMISSIONS_REQUEST_SEND_SMS);



                if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.SEND_SMS)) {


                } else {



                    ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.SEND_SMS}, MY_PERMISSIONS_REQUEST_SEND_SMS);
               }
            }
            sendSMS("6785459898","+++ Testing SMS +++");
        }
        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();

            try {
                unregisterReceiver(sendBroadcastReceiver);
                unregisterReceiver(deliveryBroadcastReceiver);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            try {
                unregisterReceiver(sendBroadcastReceiver);
                unregisterReceiver(deliveryBroadcastReceiver);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


            private void sendSMS(String ph, String msg){
                String SENT = "SMS_SENT";
                String DELIVERED = "SMS_DELIVERED";

                PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), PendingIntent.FLAG_UPDATE_CURRENT);

                PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), PendingIntent.FLAG_UPDATE_CURRENT);

                registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

                registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));

                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(ph, null, msg, sentPI, deliveredPI);

            }


        }

Here is the manifest:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.BROADCAST_SMS"/>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <receiver android:name="com.android.internal.telephony.sendsms5.DeliverReceiver">
        <intent-filter>
            <action android:name="SMS_DELIVERED" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.android.internal.telephony.sendsms5.SentReceiver">
        <intent-filter>
            <action android:name="SMS_SENT" />
        </intent-filter>
    </receiver>
 </application>

How to solve:

Try removing the intent-filters, on some OS levels implicit intents in this case don’t work

Leave a Reply

Your email address will not be published. Required fields are marked *