1

I have an application where an SMS is sent to a particular number and the delivery reports are to be tracked. I am using the code from this SO link and when the application is running, the delivery reports are coming just fine.

How do i handle the scenario, where, for example, the number to which i sent the SMS is switched off (OR) not in the network area (OR) the sms delivery is delayed for some reason. Now when the SMS is delivered, i will get the delivery report, but, i am not sure how to handle these kind of scenarios.

can someone please point me in this direction.

Note: This code is inside a Fragment

My code till now:

sendSMS method:

   private void sendSMS(JSONObject object, String sms_message) {

        try {

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

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

            activity.registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
            activity.registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(object.getString("official_mobile"), null, sms_message, sentPI, deliveredPI);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Broadcast Receivers:

class sentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Log.d(Const.DEBUG, "Message Sent Successfully");
                Toast.makeText(activity, "SMS Sent Successfully", Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Log.d(Const.DEBUG, "Message Sending Failed");
                Toast.makeText(activity, "Generic failure",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Log.d(Const.DEBUG, "Message Sending Failed");
                Toast.makeText(activity, "No service",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Log.d(Const.DEBUG, "Message Sending Failed");
                Toast.makeText(activity, "Null PDU", Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Log.d(Const.DEBUG, "Message Sending Failed");
                Toast.makeText(activity, "Radio off",
                        Toast.LENGTH_SHORT).show();
                break;
        }

    }
}


class deliverReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Log.d(Const.DEBUG, "Message Delivered Successfully");
                Toast.makeText(activity, "SMS Delivered",
                        Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Log.d(Const.DEBUG, "Message Delivery Failed");
                Toast.makeText(activity, "SMS Delivery Failed.",
                        Toast.LENGTH_SHORT).show();
                break;
        }

    }
}
Community
  • 1
  • 1
Vamsi Challa
  • 10,690
  • 30
  • 96
  • 146

0 Answers0