2012-08-06 13 views
5

Sto sviluppando un app che svolgono un'azione su:coda di SMS Gestione SMS per il fallimento

  • Ricevere SMS
  • Invio di SMS
  • Facendo qualche compito computazionale al mittente.

E 'possibile che SMS non è riuscito a inviare. Qualcuno può dirmi come gestire la coda dei messaggi SMS inviati falliti e continuare a riprovare a inviarli dopo un po 'di tempo.

Ho visto il codice ma non so come gestire la coda degli SMS e rinviarli.

Ecco il codice:

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); 
} 
+0

cosa è che vuoi aiuto? Il tuo uso di PendingIntent sembra corretto per me. – wojciii

+1

hey wojci il mio codice va bene, ma ora sto trovando il modo di avanzare ulteriormente, è possibile che sms non è riuscito a inviare al mittente, perché l'utente non ha credito in là account, quindi gestiamo una coda e controlliamo la coda dopo un certo intervallo e Restry per inviare il messaggio. grazie – Fawad

risposta

1

Non sono sicuro se ho capito la tua domanda correttamente, ma secondo me, questo sembra una soluzione più semplice:

Quando si verifica una delle avarie al di sopra, si può inserire quel numero in un elenco e controllare l'elenco dopo il limite di tempo specificato. Se un numero qualsiasi si verifica nell'elenco, quindi inviare il messaggio a quel numero. Dopo l'invio msg, è necessario rimuovere l'elemento dall'elenco

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()); 

      } 
      } 
     } 

     }; 

e nell'attività principale, svalutazione

new checkList().execute(); 
+0

grazie per la risposta u capisco correttamente la mia domanda, ma come controllare puntualmente l'elenco, per favore aiutatemi con il codice. – Fawad

+1

ma desidero controllare l'elenco ogni ora finché il messaggio non viene inviato correttamente è possibile? – Fawad

+0

U può inserire qualsiasi valore in "Thread.sleep (valore)" nel codice ... il valore dovrebbe essere in millisecondi. –

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)); 
+0

controllo bro, sto facendo qualcosa di sbagliato? – Fawad

+0

Vedere la risposta di DonCroco in questo collegamento ho suggerito u ... + AsyncTask dovrebbe essere nella parte inferiore della classe MainActivity .. –

+0

bro puoi modificare il mio codice per ciò che voglio ottenere per favore sono totalmente incasinato con AyncTask :(thanx – Fawad