Documentation Index
Fetch the complete documentation index at: https://mintlify.com/AmolPardeshi99/android-performance-skills/llms.txt
Use this file to discover all available pages before exploring further.
BroadcastReceiver.onReceive runs on the main thread and must complete within 10 seconds (foreground) or 60 seconds (background) before the system fires an ANR. The commonly misunderstood goAsync() API does not pause this timer — it still runs from the moment onReceive starts.
Heavy Work in onReceive
Any blocking call insideonReceive — network I/O, database access, file reads — holds the main thread and pushes toward the ANR threshold. The system has no patience here: input events queued during a blocked onReceive begin contributing to the 5-second input ANR clock at the same time.
Bad: network call directly in onReceive
The goAsync() Misconception
This is one of the most common mistakes in Android broadcast handling. Developers see thatgoAsync() “hands off” execution and assume the ANR clock resets or pauses. It does not. The system is measuring wall-clock time from the start of onReceive, full stop.
Subtle bug: goAsync used but finish() arrives after timeout
Correct Option A: goAsync with a Coroutine and try/finally
UsegoAsync() only for work that is reliably short — well under 10 seconds. Always call pending.finish() in a finally block so it is invoked even when an exception is thrown.
Correct Option B: Delegate to WorkManager
When the broadcast signals that background work needs to happen — syncing data, downloading a file, processing a notification payload — enqueue aOneTimeWorkRequest and return immediately. The receiver exits in microseconds.
Decision Guide
When should I use goAsync vs WorkManager?
When should I use goAsync vs WorkManager?
Use
goAsync() when:- The work is guaranteed to complete in well under 10 seconds (e.g., a quick cache update or a local database write)
- You need the result before returning from the broadcast lifecycle
- You always call
pending.finish()in afinallyblock
- The work involves network I/O, large file operations, or anything with unpredictable duration
- You need retry on failure, constraint handling (requires network, charging), or deduplication
- The broadcast is from a background context where the 60-second limit still leaves too much uncertainty