Android sendTextMessage sending two identical exception messages
I am working on an application that sends SMS messages. The problem is that the sendTextMessage method is sending two messages with the same content. How to fix it?
This class starts the process
public class C2DMMessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Some stuff
Log.i("C2DMMessageReceiver", "Got C2DM message");
SmsSend message = new SmsSend(context, phonenumber, line);
message.send()
}
}
Class for sending text messages
public class SmsSend {
SmsSend(Context tcontext, String phoneNumber, String smstext){
context = tcontext;
phone_number = phoneNumber;
message = smstext;
}
protected void send(){
if(foo){
Log.i("SmsSend", "Sending message");
SmsManager sms = SmsManager.getDefault();
String sent = "android.telephony.SmsManager.STATUS_ON_ICC_SENT";
PendingIntent piSent = PendingIntent.getBroadcast(context, 0, new Intent(sent), 0);
sms.sendTextMessage(phone_number, null, message, piSent, null);
}
}
}
to see what's going on
public class SmsSentBroadcastReciever extends BroadcastReceiver{
private static final String TAG = "SmsSentBroadcastReciever";
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()){
case Activity.RESULT_OK:
Log.i(TAG,"SMS sent");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Log.e(TAG,"Generic failure");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Log.e(TAG,"No service");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Log.e(TAG,"PDU NULL");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Log.e(TAG,"Radio off");
break;
}
}
}
Logout LogCat
C2DM message received
Sending a message
SMS sent
SMS sent
This way sendTextMessage only fires once, but it still throws two messages. What to do?
The device I'm debugging from is a Samsung Galaxy S2 with Android 4.0. I have read some old streams that sendTextMessage is corrupted on some (HTC) devices, so I tried with sendMultipartTextMessage, but it gives the same result.
source to share
The following code works fine, S2 with ICS:
void sendMessageGTI9100ICS(String number, String msg) throws Exception {
SmsManager m = SmsManager.getDefault();
Class aclass[] = new Class[9];
aclass[0] = String.class;
aclass[1] = String.class;
aclass[2] = ArrayList.class;
aclass[3] = ArrayList.class;
aclass[4] = ArrayList.class;
aclass[5] = Boolean.TYPE;
aclass[6] = Integer.TYPE;
aclass[7] = Integer.TYPE;
aclass[8] = Integer.TYPE;
Method method = m.getClass().getMethod("sendMultipartTextMessage", aclass);
Object aobj[] = new Object[9];
aobj[0] = number;
aobj[1] = null;
aobj[2] = m.divideMessage(msg);
aobj[3] = null;
aobj[4] = null;
aobj[5] = Boolean.valueOf(false);
aobj[6] = Integer.valueOf(0);
aobj[7] = Integer.valueOf(0);
aobj[8] = Integer.valueOf(0);
method.invoke(m, aobj);
}
source to share
There is a firmware issue (LPQ) specifically for Galaxy S2 where SMS is sent twice to all third party apps (including Handcent, GO SMS, etc.) except for the stock exchange app. I think the latest LP7 firmware fixes the duplicate SMS sending issue. Can you check the firmware version on your phone?
Handcent and GO SMS found their workarounds and updated their apk to the market.
source to share
This code will print "SMS sent" when activity is returned, not when sending sms.
public void onReceive(Context context, Intent intent) {
switch (getResultCode()){
case Activity.RESULT_OK:
Log.i(TAG,"SMS sent");
break;
So what is probably happening is that there is one more action that you take and then return RESULT_OK. I can't tell you though, as I can see how you've configured your broadcast receivers to receive.
The problem was with the firmware reported in the answers above.
source to share