Search code examples
androidbroadcastreceivertoast

Creating a Toast inside onReceive() method of a BroadcastReceiver


As you may know, the documentation for onReceive() method of the class BroadcastReceiver says:

When it runs on the main thread you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed). You cannot launch a popup dialog in your implementation of onReceive().

I want to display a Toast to the user when I receive a broadcast, but I'm not sure if a Toast is considered a 'popup dialog' or only objects from the class Dialog are what they are referring to.

I've seen many samples of code where Toast is used inside this function but I'm not sure if it's bad practice or if it has a great performance impact.

What I would like to know is whether or not I should create a Toast inside this function and if not then how should I do it (Extend Toast and then use an intent to call that?).


Solution

  • Toast is not considered a popup dialog. It is considered to be a notification method like the notifications in the notification bar. Many apps use it in BroadcastReceivers, including my own. Till date, I've never seen a performance impact from a Toast being displayed on any device.

    You should be perfectly fine showing Toasts from a BroadcastReceiver.