3
// ---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message) {
    int i;
    SmsManager sms = SmsManager.getDefault();
    int amount = 10; // just making 10 the default if the EditText has an
                        // invalid value
    try {
        amount = Integer.parseInt(smsamount.getText().toString());
    } catch (NumberFormatException smsamount) {
    }

    for (i = 0; i < amount; i++) {

        if (amount < 100)
            sms.sendTextMessage(phoneNumber, null, message, null, null);

        else
            Toast.makeText(getBaseContext(),
                    "Please enter an amount less than 100.",
                    Toast.LENGTH_SHORT).show();
    }

}

This works, but the toast is staying on the screen for over a minute. It stays on the screen even after exiting the app. I have tried changing the toast to before the process to send the messages, but it force closes the app. Is this something that I am going to have to use the hack located here: http://thinkandroid.wordpress.com/2010/02/19/indefinite-toast-hack/ to make it shorter?

Jasonwilliams10
  • 292
  • 1
  • 4
  • 17
  • 1
    take your toast out of loop, your else could be called few times. Or your can leave it in the loop, but in this case add some boolean flag - boolean hasShown = false; when you show it - hasShown = true; And in your else check if you had shown the Toast already before –  Nov 17 '11 at 16:43

1 Answers1

6
  1. If you call N * 1 seconds Toasts at once, then they will sequentially display for up to ~N seconds, as that happens in your inner cycle. Here you get amount * SHORT_DURATION total duration of toasts.

  2. Do the condition checking if (amount < 100) before entering the loop. This is more correct, efficient, and will display only one Toast.

ron
  • 9,262
  • 4
  • 40
  • 73