Recipe Accessing the Wireless Network

Many applications utilize the network connectivity of the Android device. To better understand how to handle application behavior due to network changes,Android provides access to the underlying network state. This is done by broadcasting intents to notify application components of changes in network connectivity and offer control over network settings and connections.

Android provides a system service through the ConnectivityManager class to let developers monitor the connectivity state, set the preferred network connection, and manage connectivity failover.This is initialized as follows:

ConnectivityManager myNetworkManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

To use the connectivity manager, the appropriate permission is needed in the Android-Manifest XML file for the application:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

The connectivity manager does provide the two methods getNetworklnfo() and getActiveNetworkInfo() to obtain the details of the current network in a Networklnfo class. However, a better way to monitor the network changes is to create a broadcast receiver, as shown in the following example:

private BroadcastReceiver mNetworkReceiver = new BroadcastReceiver(){ public void onReceive(Context c, Intent i){ Bundle b = i.getExtras(); NetworkInfo ni = (NetworkInfo)

b.get(ConnectivityManager.EXTRA_NETWORK_INFO); if(ni.isConnected()){

//do the operation }else{

//announce the user the network problem

After a broadcast receiver is defined, it can be registered to listen for ConnectivityManager.CONNECTIVITY_ACTION intents:

this.registerReceiver(mNetworkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

The mNetworkReceiver defined previous extracts only the NetworkInfo from ConnectivityManager.EXTRA_NETWORK_INFO. However the connectivity manager has more information that can be exposed.The different types of information available are collected in Table 7.3.

Table 7.3 The Possible Information from a Connectivity Manager

Type of Information



Contains additional information about network state


Returns boolean value if the current connection is the result of a failover network


Returns a NetworkInfo object


Returns boolean value if there is no network connectivity

Returns a NetworkInfo object about the available network for failover when the network is disconnected



Returns a String value that describes the reason of connection failure

The ConnectivityManager also provides the capability to control network hardware and failover preferences.The setNetworkPreference() method can be used to select a network type. To change the network, the application needs to set another permission in the AndroidManifest XML file:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

This page intentionally left blank

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment