26

I have a broadcast receiver and I am trying to show a toast message from it, is this possible ? This code doesn't show the toast but it print the log message in the logcat. Is there some idiotic thing I am doing or what is my problem ?

@Override
public void onReceive(Context context, Intent intent) {
    Log.v("log", "this is shown");
    Toast.makeText(context, "this is not shown"     , Toast.LENGTH_LONG);
}
Lukap
  • 31,523
  • 64
  • 157
  • 244
  • 2
    As lukuluku suggests, creating a `Toast` from a `BroadcastReceiver` isn't necessarily a good thing to do. It's possible your `BroadcastReceiver` may be 'alive' when some other `Activity` is in use. In this case, the `Toast` will make no sense to the user of the device. – Squonk Oct 26 '11 at 10:22
  • @Squonk sure, it's not a good thing in general, but useful in [cases like this](http://stackoverflow.com/a/23135951/504611). – Vicky Chijwani Oct 11 '16 at 20:00

3 Answers3

59

Call the show() method for the Toast.

user
  • 86,916
  • 18
  • 197
  • 190
12

you forgot to call show() on the Toast.. although i would not recommend creating toasts from BroadcastReceivers.. you might consider using Notifications

lukuluku
  • 4,344
  • 3
  • 30
  • 35
6

Use this

Toast.makeText(context, "this is not shown",Toast.LENGTH_LONG).show();

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54