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.

3 Comments

  1. Pingback: Manipulating Incoming & Outgoing Calls Tutorial For Android | Farhan's Blogs & Portal

  2. Pingback: Manipulating Incoming & Outgoing Calls Tutorial For Android | Farhan's Blogs & Portal

  3. Hi,

    Could you please suggest how to get a host name of a machine through the emulator which connected in the same LAN.
    For example: -My LINUX machine IP is 12.123.250.123 and my emulator is running another windows machine(same n/w). Is there any way to get the linux hostname through the emulator app.

    Please suggest

    Regards,
    Praveen

Leave a Reply

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

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