Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMS queue Handling for failure SMS

Tags:

android

sms

I am developing an app which perform an action on:

  • Receiving SMS
  • Sending SMS
  • Doing some computational Task to Sender.

Is it possible that SMS failed to send. Can any one tell me how to manage the queue of failed sent SMS messages and keep retrying to send them after some time.

I have seen the code but don't know how to handle queue of SMS and resend them.

Here is Code:

private void sendSMS(String phoneNumber, String message)
{        
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    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:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);   
}
like image 624
Fawad Avatar asked Nov 14 '22 00:11

Fawad


1 Answers

I am not sure if I understood your question correctly but according to me, this seems a simpler solution:

When any of the above failure occurs, you can insert that number in a List, and check the list after specified time limit. If any number occurs in list, then send Msg to that number. After sending msg, you should remove that item from List

EDIT:

code-

 class checkList extends AsyncTask<String, Void, Void> {
         public Void doInBackground(String... p) {
          while (true) {

                       //Check List Value Here

           try {
            Thread.sleep(1000);
           } catch (InterruptedException ie) {
            ie.printStackTrace();
            Log.e("Sleep", "Error: " + ie.toString());

           }
          }
        }

        };

And in main Activity, write-

new checkList().execute();
like image 145
Name is Nilay Avatar answered Jan 10 '23 19:01

Name is Nilay