0

Situation:

I have a Service (also have tried using anActivity) that sends SMS messages on each button click. I'm 99.9% certain the method sendSMS(...) is getting called only once. Toasts that are created within the BroadcastReceiver stay on the screen from anywhere between a few seconds and almost in defiantly, until app is forced stop. "SMS Sent" is stuck on the screen for a period of time and the SMS sends successfully. You can see the Toast fade in and out a little bit.

Am I doing something wrong?

Bonus:

Why doesn't the BroadcastReceiver with the "SMS delivered" status ever get any responses?

Here's the very Generic code I grabbed from a tutorial long ago:

    @Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    sendSMS("5555555","hello world");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    sendSMS("5555555","hello world");
    return Service.START_STICKY;
}

  // ---sends an SMS message to another device---
private void sendSMS(final String phoneNumber, final String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this.getApplicationContext(), 0, new Intent(
            SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this.getApplicationContext(), 0,
            new Intent(DELIVERED), 0);

    // ---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(context, "SMS error: Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(context, "SMS error: No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(context, "SMS error: Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(context, "SMS error: Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    // ---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(context, "SMS delivered",
                        Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(context, "SMS not delivered",
                        Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

/Edit: fixed the problem as you can read in the comments of this question

Community
  • 1
  • 1
StrikeForceZero
  • 2,379
  • 1
  • 25
  • 36
  • 1
    99.9 % sure means that you haven't set a breakpoint to the `sendSMS(...)` method and checked that it's only being called once yet? – Ben Weiss Sep 07 '12 at 09:00
  • @Keyboardsurfer Touche, yes I have not. After setting some break points I found out my service is calling it twice via its `onStart()` and `onStartCommand()` I have both for compatibility I didn't know they were both called tho.. ideas? and why would that make the toast visible for soo long anyway? – StrikeForceZero Sep 07 '12 at 09:16
  • Defiantly was the problem, I commented out the deprecated method `onStart()` and the Toast disappeared normally – StrikeForceZero Sep 07 '12 at 09:23

2 Answers2

0

Was calling sendSMS() twice, culprit was deprecated onStart() method for my service

@Override
public void onStart(Intent intent, int startId) {
    //super.onStart(intent, startId);
    sendSMS("5555555","hello world");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //super.onStartCommand(intent, flags, startId);
    sendSMS("5555555","hello world");
    return Service.START_STICKY;
}

This answer helped me keep both methods

Community
  • 1
  • 1
StrikeForceZero
  • 2,379
  • 1
  • 25
  • 36
-3

use this to show

Toast.makeText(context,message, 1000).show();

where 1000 is time duration in miliseconds

Rajendra
  • 1,700
  • 14
  • 17
  • Eclipse says: "Expected duration Toast.LENGTH_SHORT or Toast.LENGTH_LONG, a custom duration value is not supported" I'm assuming this shouldn't be practiced? – StrikeForceZero Sep 07 '12 at 09:21
  • It just won't work. I don't have the link handy but some other StackOverflow answer cites the Android source that reads that param, and all it does is check (duration == LENGTH_LONG). LENGTH_SHORT is 0 and LENGTH_LONG is 1, they are just flags. – AlcubierreDrive Jan 07 '13 at 09:31