I've been working on an app that sends SMS-messages. The problem I have is that the sendTextMessage method sends two messages with the same content. How do I fix that?
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);
}
}
}
class to find out what's happining
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;
}
}
}
The output from LogCat is
Got C2DM message
Sending message
SMS sent
SMS sent
So the sendTextMessage is only fired once but it still throws two messages. What to do?
The device I'm debugging with is a Samsung Galaxy S2 with Android 4.0. I read some old threads that sendTextMessage is broken on some (HTC) devices so I tried with sendMultipartTextMessage but it gives the same result.
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);
}
I've created a small Android library project that solves this issue using the reply from Stipa as well as code to make it work on HTC Tattoo and to make sure the right implementation is selected based on the device.
https://github.com/nadam/compatibility-sms-manager
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With