【问题标题】:SMS queue Handling for failure SMS短信队列 失败短信处理
【发布时间】:2012-08-03 03:14:57
【问题描述】:

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

  • 接收短信
  • 发送短信
  • 为发件人执行一些计算任务。

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

我看过代码,但不知道如何处理 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);   
}

【问题讨论】:

  • 您需要什么帮助?在我看来,您对 PendingIntent 的使用是正确的。
  • 嘿 wojci 我的代码很好,但现在我正在寻找进一步推进的方法,短信可能无法发送到发件人,因为用户在该帐户中没有信用,所以我们管理一个队列并在一段时间后检查队列并重新发送消息。谢谢

标签: android sms


【解决方案1】:

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

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

编辑:

代码-

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

           }
          }
        }

        };

在主Activity中,写-

new checkList().execute();

【讨论】:

  • 感谢回复你正确理解我的问题,但是如何及时检查列表,请帮助代码。
  • 但是兄弟我想每隔一小时检查一次列表,直到消息成功发送这可能吗?
  • 你可以在代码中的“Thread.sleep(value)”中放入任何值...值应该以毫秒为单位。
  • 是的,当然有可能。
  • 谢谢兄弟,我会尝试代码并让你知道,而且兄弟我不能给你加 1,因为它需要 15 个代表,我的目前是 6 个。
【解决方案2】:
        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));

【讨论】:

  • 在我建议你的那个链接中查看 DonCroco 的答案...+ AsyncTask 应该在 MainActivity 类的底部..
  • 兄弟你能把我的代码修改成我想要达到的效果吗?我完全被 AyncTask 搞砸了 :(thanx
猜你喜欢
  • 1970-01-01
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
相关资源
最近更新 更多