Android network connectivity BroadcastReceiver

Download source code of this article.

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 ConnectivityManager.EXTRA_NETWORK_INFO key.

2 Responses to “Android network connectivity BroadcastReceiver”

  1. yousef

    Can we have app that listen and react anytime device connect to internet. for example, anytime device connect to internet automatically pars xml and notify to user.

    Reply

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>