5

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);   
}
wojciii
  • 4,253
  • 1
  • 30
  • 39
Fawad
  • 137
  • 1
  • 9
  • What is that you want help with? Your use of PendingIntent looks correct to me. – wojciii Aug 06 '12 at 11:10
  • 1
    hey wojci mine code is fine but now i am finding way to advance further, it is possible that sms failed to send to Sender , because user have no credit in there account , so we manage a queue and check the queue after some interval and restry to send the message . thanx – Fawad Aug 06 '12 at 11:54

2 Answers2

1

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();
Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
0
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phone_nr, null,"dummy text" , sentPI, null); 

             class checkList extends AsyncTask<String, Void, Void> {
         public Void doInBackground(String... p) {
          while (true) {
                   //failed msg send Again
               sms.sendTextMessage(phone_nr, null,"message Sended" , sentPI, null); 

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

           }
          }
        }

        };

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                            //if failed msg sended cancel the AsyncTask
                            new checkList().cancel();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    new checkList().execute();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    new checkList().execute();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    new checkList().execute();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    new checkList().execute();
                    break;
            }
        }
    }, new IntentFilter(SENT));
Fawad
  • 137
  • 1
  • 9
  • See DonCroco's answer in that link i suggested u...+ AsyncTask should be at the bottom of the MainActivity class.. – Name is Nilay Aug 07 '12 at 04:50
  • bro can u modify my code to what i want to achieve please i am totally messed with AyncTask :( thanx – Fawad Aug 07 '12 at 05:37