Most of the time we need to connect to internet to download or upload data. If the downloading or uploading is a lengthy process, we need to know when the network connected or gone, so that we can temporarily pause the uploading or downloading and resume once the connection is established. In Android we can register a
BroadcastReciver with action
ConnectivityManager.CONNECTIVITY_ACTION. System will broadcast this whenever a change in the network connectivity occurs.
Information regarding the connection will be there in the receiving intent. Some of them are:
ConnectivityManager.EXTRA_EXTRA_INFO A string value indicating the network state. ConnectivityManager.EXTRA_IS_FAILOVER A boolean value indicates whether the connection manager is failing over or not. ConnectivityManager.EXTRA_NETWORK_INFO A NetworkInfo class with network information. ConnectivityManager.EXTRA_NO_CONNECTIVITY A boolean value indicating there is no internet connectivity. ConnectivityManager.EXTRA_OTHER_NETWORK_INFO A NetworkInfo class with network information of another network that may be connected. ConnectivityManager.EXTRA_REASON A String value about the reason of the connection failure.
You can check either
ConnectivityManager.EXTRA_EXTRA_INFO or NetworkInfo.State from the