Android Detect Internet Connection Using Broadcast Receiver

Android Detect Internet Connection Using Broadcast Receiver

Android lets your application connect to the internet or any other local network and allows you to perform network operations.

A device can have various types of network connections. If your app needs internet connection to make HTTP Requests and or you need internet connection in your whole app then it is better to check internet connectivity status before making any HTTP Requests to avoid http exceptions.

Checking Network Connection

Before you perform any network operations, you must first check that are you connected to that network or internet. For this android provides ConnectivityManager class. You need to instantiate an object of this class by calling getSystemService() method. Its syntax is given below −

Once you instantiate the object of ConnectivityManager class, you can use getAllNetworkInfo()  or getActiveNetworkInfo() method to get the information of all or active networks respectively. First method returns an array of NetworkInfo and second method return active network info. So you have to receive it like this –

The last thing you need to do is to check Connected State of the network. Its syntax is given below −

Apart from this connected states, there are other states a network can achieve. They are listed below:

  1. Connecting.
  2. Disconnected.
  3. Disconnecting.
  4. Suspended.
  5. Unknown.

Now, since we are using Broadcast Receiver to detect the internet connection then we should know about Broadcast Receiver.

Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.

Example

In this tutorial, we are going to learn how to detect Internet connection in whole app using Broadcast Receiver.

Video Demo

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. After creating first step the second thing we have to do is adding certain permissions to Manifest file for accessing Network State.

3. After putting permissions we have to put our receiver also in manifest file with some intent filters and make sure to enable it.

4. Now, you manifest file would be like this.

5. Now, create a simple layout naming activity_main.xml and this layout contain only one TextView to display Internet Status.

6. When we are using Broadcast Receiver it will always trigger when internet is not there if your activity is closed. So for this we have to make an java class naming MyApplication.java extended by Application. This class is used to check if activity is visible or not and trigger receiver if activity is visible.

7. Now create a another class for broadcast receiver naming InternetConnection_Receiver.java extended by BroadcastReceiver. In this class we get the boolean value from the MyApplication class to check whether the activity is visible or not and check network status if activity is visible else do nothing.

If activity is visible then change the status of TextView that we created in activity_main.xml layout.

8. Finally come to your MainActivity.java and add the following code. As you are seeing that i had added network connection code at starting to check internet status at starting and change TextView text according to that. And we had created a method that is used to change the TextView status according to boolean value it received.

Also we have to set activity visibility by calling MyApplication on onPause() and onResume() method. This will set true and false value to MyApplication boolean variable.

Full Source code of MainActivity.java.

9. Now, you can check internet connection at a particular point or in whole app according to your need.

Thanks. :)

Download Source Code – >

 

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)