SmsManager 不断重试在 HTC Desire 上发送短信
在我的应用程序中,我需要发送短信,因此我使用以下代码,
final String SMS_REQUEST_OK = "SMS_REQUEST_OK";
String m_sms_message = String.format("sample text");
String m_dest_number = "some number";
Intent SMSInfo = new Intent(SMS_REQUEST_OK);
SMSInfo.putExtra("msg", m_sms_message);
SMSInfo.putExtra("num", m_dest_number );
PendingIntent sentPI = PendingIntent.getBroadcast(m_context, 0,
SMSInfo, PendingIntent.FLAG_CANCEL_CURRENT);
m_context.registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(m_context, String.format(
m_context.getResources().getString(R.string.sms_success),
arg1.getExtras().getString("msg"),
arg1.getExtras().getString("num")),
Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(m_context, String.format(
m_context.getResources().getString(R.string.sms_error),
arg1.getExtras().getString("msg"),
arg1.getExtras().getString("num")),
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(SMS_REQUEST_OK));
SmsManager.getDefault().sendTextMessage(
m_dest_number,
null,
m_sms_message,
sentPI,
null);
我希望它尝试发送一次短信,然后显示带有此操作结果的 Toast 消息。如果 SMS 发送成功,它工作正常,但如果没有成功,它会不断重试发送它,并判断大量 Toast 消息(它发生在 HTC Desire (S) 上,在 Samsung 上测试它不会出现此行为 - Toast 有错误显示一次)。 那么 - SmsManager
应该如何表现以及如何避免它(以便它仅尝试发送短信一次)?
编辑我忘了提及 - 如果getResultCode()
返回133404,就会发生这种情况,还没有对其他错误进行测试
In my app I need to send SMS, so I use the following code
final String SMS_REQUEST_OK = "SMS_REQUEST_OK";
String m_sms_message = String.format("sample text");
String m_dest_number = "some number";
Intent SMSInfo = new Intent(SMS_REQUEST_OK);
SMSInfo.putExtra("msg", m_sms_message);
SMSInfo.putExtra("num", m_dest_number );
PendingIntent sentPI = PendingIntent.getBroadcast(m_context, 0,
SMSInfo, PendingIntent.FLAG_CANCEL_CURRENT);
m_context.registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(m_context, String.format(
m_context.getResources().getString(R.string.sms_success),
arg1.getExtras().getString("msg"),
arg1.getExtras().getString("num")),
Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(m_context, String.format(
m_context.getResources().getString(R.string.sms_error),
arg1.getExtras().getString("msg"),
arg1.getExtras().getString("num")),
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(SMS_REQUEST_OK));
SmsManager.getDefault().sendTextMessage(
m_dest_number,
null,
m_sms_message,
sentPI,
null);
I expect it to try to send SMS one time, and then show Toast message with result of this operation. It works fine if SMS is sent successfully, however if it's not it keeps retrying to send it, judging on lots of Toast messages (it occurs on HTC Desire (S), testing it on Samsung doesn't get this behaviour - Toast with error is shown once).
So - is it how SmsManager
should behave and how to avoid it (so that it try to send SMS only one time)?
edit I forgot to mention - it happens if getResultCode()
returns 133404, haven't tested it on other errors
edit2 According to this, 133404
is htc-specific error, which means temporary failure and device will retry automatically, untill, eventually, proper result code is received and broadcast is sent. However, no SmsManager-documented broadcast is received within resonable time. So the question remains - is there a way to stop this retry attempts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是总结一下我发现的内容:根据 android 文档,sendTextMessage() 的预期行为只是发送消息的一次尝试,然后广播结果。但是,某些 HTC 设备会不断重试发送消息,并在每次尝试后广播“临时错误”代码。因此,如果应用程序仅检查结果代码是否“成功”(就像 Android 市场上的许多短信应用程序似乎所做的那样),它会将消息标记为未发送并停止侦听(这符合 sendTextMessage() 函数的预期行为),这可能会导致将消息标记为未发送,而实际上在稍后的尝试中已成功发送。
更糟糕的是,记录的代码可能永远不会发送 - 因此,如果您收到 HTC“临时代码”之一(133404 或 133179,但可能还有更多)消息状态不确定 - 它可能会在一段时间后发送,或者可能永远不会发送被发送。因此,在我看来,您只能通过继续监听广播直到获得正确的结果代码(可能永远不会发送)或判断传递广播来确定消息状态。
Just to summarize what I've found out: according to android documentation, expected behaviour of sendTextMessage() is only one attempt to send message, and broadcasting result afterwards. However, some HTC devices keep retrying to send message, broadcasting "temporary error" code after each attempt. So if app is only checking whether result code is "successfull" or not (as lots of sms applications on android market seem to do), it will mark message as unsent and stop listenning to it (which suits expected behaviour of sendTextMessage() function), which may lead to marking message unsent, when actually it was sent successfully in later attempt.
What's worse is that documented code might never be sent - so, if you get one of HTC "temporary codes" (133404 or 133179, there might be more however) message status is indetermined - it might be sent some time later, or might never be sent. So as it seems to me you can only determine message status by keeping listening for broadcast until you get proper result code (which may never be sent), or judging on delivery broadcast.