Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android sendTextMessage sends two identical messages on exceution

Tags:

android

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.

like image 316
daker Avatar asked Sep 15 '25 03:09

daker


2 Answers

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);
}
like image 69
lstipakov Avatar answered Sep 16 '25 17:09

lstipakov


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

like image 33
Adam Nybäck Avatar answered Sep 16 '25 18:09

Adam Nybäck