Many of the application involves some service running when there is network present or there is a internet access for that we should know how to monitor the network state
Android provides a system service called
ConnectivityManager that allows us to subscribe to notifications when network state is changed. Network connectivity changes can be due one of the following events:- Current network interface being disconnected
- A new network interface being connected
- Handover (also known as “fallover”) between two network interfaces (usually as a result of one of the two events above), such as WiFi being activated as a result of user coming home and all network traffic will be routed through WiFi as opposed to 3G.
We can use
BroadcastReceivers to subscribe to network connectivity changes as follows:Create a BroadcastReceiver that will handle connectivity status notifications
TheBroadcastReceiverwill be notified whenever the network status changes with the following information:ConnectivityManager.EXTRA_NETWORK_INFO– A NetworkInfo object with network information that caused the status change.ConnectivityManager.EXTRA_REASON- A String value about the reason of the connection failure, which can benull.ConnectivityManager.EXTRA_IS_FAILOVER– A boolean value indicating whether the connection manager is failing over from a disconnected network or not.ConnectivityManager.EXTRA_NO_CONNECTIVITY– A boolean value indicating there is no internet connectivity.ConnectivityManager.EXTRA_EXTRA_INFO– A string value indicating the network state.ConnectivityManager.EXTRA_OTHER_NETWORK_INFO– An optional field that contains NetworkInfo describing properties of a new network that is connected as a result of loss of connectivity
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);if(noConnectivity){isConnected=false;}else if(noConnectivity==false){isConnected=true;}// do application-specific task(s) based on the current network state, such// as enabling queuing of HTTP requests when currentNetworkInfo is connected etc.}
};
More details about theCONNECTIVITY_ACTIONcan be found hereRegister the BroadcastReceiver within the application
The next step is to register the BroadcastReceiver created in the previous step, so our application will actually receive network state change notifications.
public class MainActivity extends Activity {// rest of the code in the Activity are committed for clarity/** method to be invoked to register the receiver*/private void registerReceivers() {registerReceiver(mConnReceiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));}Add the necessary permissions to your application manifest.
Need to add theandroid.permission.ACCESS_NETWORK_STATEpermission to the application’s Manifest (AndroidManifest.xml) file to enable the application to consume theConnectivityManagerservice.<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
Any queries drop the comments!!!!