Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid sendTextMessage sends two identical messages on exceution
    text
    copied!<p>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?</p> <p>This class starts the process </p> <pre><code>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() } } </code></pre> <p>Class for sending text messages</p> <pre><code>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); } } } </code></pre> <p>class to find out what's happining</p> <pre><code>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; } } } </code></pre> <p>The output from LogCat is</p> <p>Got C2DM message</p> <p>Sending message</p> <p>SMS sent</p> <p>SMS sent</p> <p>So the sendTextMessage is only fired once but it still throws two messages. What to do?</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload