Vladimir Vladimir - 2 months ago 27
Android Question

SmsManager keeps retrying to send SMS on HTC Desire

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?

Answer Source

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.