我正在开发一个应用程序,该应用程序执行以下操作:
SMS是否可能无法发送。任何人都可以告诉我如何管理失败的已发送短信的队列,并在一段时间后继续重试发送。
我已经看过代码但不知道如何处理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);
}
答案 0 :(得分:1)
我不确定我是否理解你的问题,但据我说,这似乎是一个更简单的解决方案:
发生上述任何一种故障时,您可以在List中插入该号码,并在指定的时间限制后检查列表。如果列表中出现任何数字,则将Msg发送到该数字。发送消息后,您应该从列表中删除该项
编辑:
代码 -
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();
答案 1 :(得分: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));