Question

My app to app send SMS has a fairly standard loop to iterate through a list of phone numbers and use SMSManager.sendMultipartTextMessage().

A BroadcastReceiver (one per message part with a unique PendingIntent, the receiver filters on that intent) is used to record any failures.

I cannot get the issue to re-create for me but I know some of my users are getting multiple generic failures (SmsManager.RESULT_ERROR_GENERIC_FAILURE).

I believe that their phones are unable to handle multiple SMSs coming through at high speed. Even if I put a delay (not a nice solution) into my loop I think that the SMS still somehow stack up in the phone and then suddenly flood through - causing the generic failures.

I know many others get this issue but I have seen no useful solutions other than for the very simple case (no error flows etc.).

I thought of an infinite loop that waited until each BroadcastReceiver.onReceive() reported back (a listener effectively) but I hate the idea of an infinite loop (and trying to manage it for all error flows as well).

Any ideas for how I could ensure that the SMS process one at a time (throttle)?

Was it helpful?

Solution

Use the pending intent to wait for the message's status(generic error/radio off / null PDU/ SMS delivered / SMS Undelivered ).

SmsManager smsManager=SmsManager.getDefault();
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";     
public void sendSms(String phoneNumber,String sms){

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

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

 //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    // do what you need to do if OK
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    // do what you need to do if GENERIC FAILURE
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    // do what you need to do if no Service
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:

                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:

                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    // Send next sms when previous one delivered your choice

                    break;
                case Activity.RESULT_CANCELED:
                    //Do something if not delivered
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));


    smsManager.sendTextMessage(phoneNumber, null, sms, sentPI, deliveredPI);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top