if you are developing an Android app you may already fetching 
information from internet. While doing so there is a chance that 
internet connection is not available on users handset. Hence its always a
 good idea to check the network state before performing any task that 
requires internet connection.
You might also want to check what kind of internet connection is available in handset. For example is wifi currently enabled? or is mobile data network is connected.
First we need following permission in order to access network state. Add following permission to your AndroidManifest.xml file.
Permissions required to access network state:
Now check following utility class 
There is also method
NetworkUtil.java
You can use this utility class in your android app to check the network state of the device at any moment.
Now this code will return you the current network state whenever the utility method is called. What if you want to do something in your android app when network state changes? Lets say when Wifi is disabled, you need to put your android app service to sleep so that it does not perform certain task. Now this is just one usecase. The idea is to create a hook which gets called whenever network state changes. And you can write your custom code in this hook to handle the change in network state.
Check
NetworkChangeReceiver.java
Once
 we define our BroadcastReceiver, we need to define the same in 
AndroidMenifest.xml file. Add following to your menifest file.
We defined our broadcast receiver class in menifest file. Also we defined two intent 
Below is complete AndroidMenifest.xml file.
AndroidMenifest.xml
Run this demo in android emulator  or actual device.
When Wifi is enabled, you’ll see a Toast message with message.
                                                  
   
Now disable Wifi. The toast message will show you message that internet connection is not available.
                                                 
        
Now enable mobile data network. The same will be show in toast message as soon as you enable mobile data connection.
                                                 
GitHub: https://github.com/viralpatel/android-network-change-detect-example
Download complete source code:
Download: android-network-change-detect-example.zip (376 KB)
You might also want to check what kind of internet connection is available in handset. For example is wifi currently enabled? or is mobile data network is connected.
Check Internet Connection
Here is a simple code snippet that will help you identify what kind of internet connection a user has on her device.First we need following permission in order to access network state. Add following permission to your AndroidManifest.xml file.
Permissions required to access network state:
| <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/> | 
NetworkUtil. It has method getConnectivityStatus which returns an int constant depending on current network connection. If wifi is enabled, this method will return TYPE_WIFI. Similarly for mobile data network is returns TYPE_MOBILE. You got the idea!!There is also method
getConnectivityStatusString which returns current network state as a more readable string.NetworkUtil.java
| packagenet.viralpatel.network;importandroid.content.Context;importandroid.net.ConnectivityManager;importandroid.net.NetworkInfo;publicclassNetworkUtil {        publicstaticintTYPE_WIFI = 1;    publicstaticintTYPE_MOBILE = 2;    publicstaticintTYPE_NOT_CONNECTED = 0;            publicstaticintgetConnectivityStatus(Context context) {        ConnectivityManager cm = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();        if(null!= activeNetwork) {            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)                returnTYPE_WIFI;                        if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)                returnTYPE_MOBILE;        }         returnTYPE_NOT_CONNECTED;    }        publicstaticString getConnectivityStatusString(Context context) {        intconn = NetworkUtil.getConnectivityStatus(context);        String status = null;        if(conn == NetworkUtil.TYPE_WIFI) {            status = "Wifi enabled";        } elseif(conn == NetworkUtil.TYPE_MOBILE) {            status = "Mobile data enabled";        } elseif(conn == NetworkUtil.TYPE_NOT_CONNECTED) {            status = "Not connected to Internet";        }        returnstatus;    }} | 
Now this code will return you the current network state whenever the utility method is called. What if you want to do something in your android app when network state changes? Lets say when Wifi is disabled, you need to put your android app service to sleep so that it does not perform certain task. Now this is just one usecase. The idea is to create a hook which gets called whenever network state changes. And you can write your custom code in this hook to handle the change in network state.
Broadcast Receiver to handle changes in Network state
You can easily handle the changes in network state by creating your own Broadcast Receiver. Following is a broadcast receiver class where we handle the changes in network.Check
onReceive() 
method. This method will be called when state of network changes. Here 
we are just creating a Toast message and displaying current network 
state. You can write your custom code in here to handle changes in 
connection state.NetworkChangeReceiver.java
| packagenet.viralpatel.network;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.widget.Toast;publicclassNetworkChangeReceiver extendsBroadcastReceiver {    @Override    publicvoidonReceive(finalContext context, finalIntent intent) {        String status = NetworkUtil.getConnectivityStatusString(context);        Toast.makeText(context, status, Toast.LENGTH_LONG).show();    }} | 
| <application...>     ...        <receiver            android:name="net.viralpatel.network.NetworkChangeReceiver"            android:label="NetworkChangeReceiver">            <intent-filter>                <actionandroid:name="android.net.conn.CONNECTIVITY_CHANGE"/>                <actionandroid:name="android.net.wifi.WIFI_STATE_CHANGED"/>            </intent-filter>        </receiver>      ...</application> | 
CONNECTIVITY_CHANGE and WIFI_STATE_CHANGED.
 Thus this will register our receiver for given intents. Whenever there 
is change in network state, android will fire these intents and our 
broadcast receiver will be called.Below is complete AndroidMenifest.xml file.
AndroidMenifest.xml
| <manifestxmlns:android="http://schemas.android.com/apk/res/android"    package="net.viralpatel.network"    android:versionCode="1"    android:versionName="1.0">    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15"/>    <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme">        <receiver            android:name="net.viralpatel.network.NetworkChangeReceiver"            android:label="NetworkChangeReceiver">            <intent-filter>                <actionandroid:name="android.net.conn.CONNECTIVITY_CHANGE"/>                <actionandroid:name="android.net.wifi.WIFI_STATE_CHANGED"/>            </intent-filter>        </receiver>    </application></manifest> | 
When Wifi is enabled, you’ll see a Toast message with message.
 
   Now disable Wifi. The toast message will show you message that internet connection is not available.
 
        Now enable mobile data network. The same will be show in toast message as soon as you enable mobile data connection.

Download Source Code
Browse through the source code in following Git repository:GitHub: https://github.com/viralpatel/android-network-change-detect-example
Download complete source code:
Download: android-network-change-detect-example.zip (376 KB)
