Broadcast receiver for checking internet connection in android app
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Android apps often need to know when the device regains or loses internet connectivity so they can retry work, refresh data, or show offline UI. Older examples use a BroadcastReceiver for everything, but modern Android requires a more careful approach because background broadcast behavior changed over time. The right answer depends on whether you need screen-level updates or long-lived app-wide monitoring.
What a Broadcast Receiver Can Still Do Well
A BroadcastReceiver is useful when a visible screen wants short-lived connectivity updates. Register it dynamically while the UI is active, do a quick connectivity check, and then let the view model or repository decide what to do.
The important detail is checking for validated internet access, not just the presence of some network link.
Register It in the Right Lifecycle
For an activity or fragment, register in onStart and unregister in onStop.
This keeps the receiver scoped to a visible screen and avoids leaks.
Why NetworkCallback Is Often Better
For modern Android, ConnectivityManager.NetworkCallback is usually a better long-term mechanism than relying only on connectivity broadcasts.
Use this when you want a cleaner app-level observer rather than a broadcast-oriented pattern.
Manifest Permissions Still Matter
At minimum, declare network state permission:
ACCESS_NETWORK_STATE lets you inspect connectivity. INTERNET lets you perform network operations. They solve different problems.
Connect the Signal to Real App Behavior
Connectivity checks are only useful if they drive a sensible retry strategy. Good patterns include:
- queue work locally while offline
- retry sync when connectivity returns
- show cached content immediately
- keep side effects idempotent so reconnect events do not duplicate writes
Do not put heavy work directly inside onReceive. Use the receiver or callback to signal a higher-level component.
Common Pitfalls
- Treating any active network as guaranteed internet access.
- Registering receivers globally when the need is only screen-specific.
- Doing database or network work directly inside
onReceive. - Forgetting to unregister callbacks or receivers.
- Relying on outdated connectivity-broadcast behavior for all Android versions.
Summary
- A dynamic
BroadcastReceiveris fine for short-lived UI connectivity updates. - '
NetworkCallbackis often the better modern choice for ongoing monitoring.' - Check for validated internet access, not just link presence.
- Keep receivers lightweight and let repositories or view models handle real work.
- Pair connectivity events with an offline-first retry strategy instead of only showing a status label.

