Notify when android device network status changes

There are a lot of applications in the play store that notifies you when your mobile goes offline. This article will help you understand how all that happens. How app knows that device is offline and notifies the user about the same by showing a notification on the screen. To understand this clearly I have created this small project on github.

How do we show the notification to the user when device goes offline

Your android device has capability to notify all the applications in the mobile when network status changes. The only thing your application needs to do is listen to that event. For that we need to create a broadcast receiver which can listen to this particular network status change event and take some action based on the network status.

For detailed explanation with demo, watch this

NetworkStateChangeReceiver

NetworkStateChangeReceiver is a broadcast receiver which can listen to network status change event. When android triggers that event then NetworkStateChangeReceiver’s onReceive() method gets called. In onReceive() method you can write the logic to identify the network status and take some action.

isConnectedToInternet() method takes care of identifying wether the device is connected to internet or not. It does that using ConnectivityManager which has the information about the active network. You can use that NetworkInfo object to check the network status as shown in the code.

Adding receiver to AndroidManifest

Now that our NetworkStateChangeReceiver is ready and it can identify the network status but where is the code which tells that this receiver will listen to the network connectivity event. The answer is in the AndroidManifest.xml file. Your AndroidManifest will have that code. Lets take a look at the AndroidManifest file.

There are two things to notice here.

  1. we have registered the receiver and added an intent filter which means that whenever an intent is fired by android which has android.net.conn.CONNECTIVITY_CHANGE action, our receiver will handle that intent.
  2. I have given three user permissions and all of them are to gain the network status access. Its very important to give these permissions otherwise you code will not work.

Show Notification

Now, we have our broadcast receiver in place and it can tell about the current internet status. But to show a notification we need UI and our NetworkStatusChangeReceiver doesn’t have a UI. To show the notification we need to pass a message to the current activity that show a notification with the network status.

To do that there should be a communication between Broadcast Receiver and the activity and to handle that we can broadcast a local message to the whole app and whichever activity is listening to that message will show the notification. In our case MainActivity is listening to this message and will show the notification using a Snackbar.

Our main activity has registered to a broadcast which has com.ajit.singh.NetworkAvailable action because thats the action we are using to broadcast the intent from the NetworkStatusChangeReceiver. In its onReceive() method we are getting the status of the network from the intent which we had set in the NetworkStatusChangeReceiver. Then final thing is show that status using a Snackbar.

Thats all folks, I hope you liked this post. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *