5

我正在开发一个执行以下操作的应用程序:

  • 接收短信
  • 发送短信
  • 对发件人做一些计算任务。

有没有可能是短信发送失败。谁能告诉我如何管理发送失败的 SMS 消息队列并在一段时间后继续重试发送它们。

我已经看过代码,但不知道如何处理短信队列并重新发送它们。

这是代码:

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);   
}
4

2 回答 2

1

我不确定我是否正确理解了您的问题,但根据我的说法,这似乎是一个更简单的解决方案:

当出现上述任何一种故障时,您可以将该号码插入一个列表中,并在指定的时间限制后检查该列表。如果列表中出现任何号码,则向该号码发送消息。发送消息后,您应该从列表中删除该项目

编辑:

代码-

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

           }
          }
        }

        };

在主要活动中,写-

new checkList().execute();
于 2012-08-06T11:18:16.040 回答
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));
于 2012-08-06T13:36:29.297 回答