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.
In Android development, maintaining an awareness of network connectivity status is crucial for apps that rely on internet availability. A common approach to managing network changes effectively is to implement a broadcast receiver specifically designed for monitoring internet connectivity.
Understanding Broadcast Receivers
A Broadcast Receiver in Android is an application component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for instance, announcements that the timezone has changed, or that the device battery is low. Apps can also initiate broadcasts—for example, to let other apps know that some data has been downloaded to the device and is available for them to use.
Use Case: Monitoring Network Connectivity
For applications that depend on network availability to operate properly, monitoring connectivity can significantly enhance user experience. By recognizing changes in network state, an app can react appropriately—by notifying the user, delaying network operations, or executing an alternative plan.
Implementing a Broadcast Receiver for Internet Connectivity
Here's how you can create a broadcast receiver that listens for changes in network status:
Permissions
First, you must include the necessary permissions in your AndroidManifest.xml:
This permission allows the app to access information about network connectivity states.
Defining the Receiver
Next, define the receiver in the manifest file:
This snippet defines a receiver class NetworkChangeReceiver which will be triggered on any connectivity change.
The Receiver Class
In the NetworkChangeReceiver class, override the onReceive() method, which handles the broadcast:
This method checks if the device is connected to the internet and displays the appropriate message.
Android API Changes
From Android N (API 24) onward, receiving broadcasts for CONNECTIVITY_ACTION became deprecated for apps targeting API level 24 and higher. For these cases, it is recommended to use ConnectivityManager.NetworkCallback for listening to network changes.
Best Practices and Enhancements
- Using NetworkCallback for New Apps: For new applications or ones targeting recent API levels, employ
NetworkCallbackinstead of broadcasting receivers for more efficiency and reliability. - Optimization: Reduce the use of background services for checking network state. Instead, consider checking connectivity when your app is active or about to perform network operations.
- User Experience: Design your app to gracefully handle the absence of network connection. Offer offline functionality or cached content when possible.
Summary Table
| Aspect | Details |
| Component Used | Broadcast Receiver, ConnectivityManager.NetworkCallback |
| Permissions Required | ACCESS_NETWORK_STATE |
| API Level Considerations | CONNECTIVITY_ACTION deprecated in API level 24+ |
| Best Practice | Use NetworkCallback for applications targeting API level 24 and above |
| User Experience | Handle no connectivity gracefully; offer offline modes or caching |
Conclusion
Monitoring internet connectivity in an Android app can significantly influence the user experience and operational efficiency. Choose the right approach based on the app's target API level and intended user base to ensure responsiveness and robust performance.

