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:

A string value indicating the network state.

A boolean value indicates whether the connection manager is failing over or not.

A NetworkInfo class with network information.

A boolean value indicating there is no internet connectivity.

A NetworkInfo class with network information of another network that may be connected.

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

  1. yousef July 6, 2013 / 11:38 am

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *