VSB VSB - 2 months ago 7
Android Question

Android: BroadcastReceiver Time limit

Are there any time limits defined for actions run inside a BroadcastReceiver.onReceive method?


onReceive() is called on the main application thread, the same thread that drives your UI. In general, you want onReceive() to return in under a millisecond, in case your UI is in the foreground, so you do not freeze the UI (a.k.a., have "jank"). There is also a 5-10 second limit, after which Android will basically crash your app.

However, you cannot reliably fork a background thread from onReceive(), as once onReceive() returns, your process might be terminated, if you are not in the foreground.

For a manifest-registered receiver, a typical pattern is to have onReceive() delegate the work to an IntentService, which has its own background thread and, being a service, tells the OS that your process is still doing some work and should let your process run a bit longer.