Monitoring Network Connectivity

One of the most useful functions of the Connectivity Manager is to notify applications of changes in network connectivity.

To monitor network connectivity create your own Broadcast Receiver implementation that listens for ConnectivityManager.CONNECTIVITY_ACTION broadcast Intents. Such Intents include several extras that provide additional details on the change to the connectivity state. You can access each extra using one of the static constants available from the ConnectivityManager class:

> extra_is_failover A Boolean that returns true if the current connection is the result of a failover from a preferred network.

> extra_no_connectivity A Boolean that returns true if the device is not connected to any network.

> extra_reason If the associated broadcast represents a connection failure, this string value includes a description of why the connection attempt failed.

> extra_network_info Returns a Networkinfo object containing more fine-grained details about the network associated with the current connectivity event.

> extra_other_network_info After a network disconnection this value will return a Networkinfo object populated with the details for the possible failover network connection.

> extra_extra_info Contains additional network-specific extra connection details.

MANAGING YOUR WI-FI

The wifiManager represents the Android Wi-Fi Connectivity Service. It can be used to configure Wi-Fi network connections, manage the current Wi-Fi connection, scan for access points, and monitor changes in Wi-Fi connectivity.

As with the Connectivity Manager, you access the Wi-Fi Manager using the getSystemService method, passing in the Context.wiFi_SERViCE constant, as shown in Listing 13-15.

LISTING 13-15: Accessing the Wi-Fi Manager Available for download on String service = Context.WIFI_SERVICE;

Wrax.com WifiManager wifi = (WifiManager)getSystemService(service);

To use the Wi-Fi Manager your application must have uses-permissions for accessing and changing the Wi-Fi state included in its manifest.

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

You can use the Wi-Fi Manager to enable or disable your Wi-Fi hardware using the setwifiEnabled method, or request the current Wi-Fi state using the getWifiState or isWifiEnabled methods, as shown in Listing 13-16.

LISTING 13-16: Monitoring and changing Wi-Fi state

Available for downloadon if (!wifi.isWifiEnabled())

Wrox.com if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING)

wifi.setWifiEnabled(true),

The following sections begin with tracking the current Wi-Fi connection status and monitoring changes in signal strength. Later you'll also learn how to scan for and connect to specific access points.

These functions are likely to be sufficient for most application developers, but the WifiManager does also provide low-level access to the Wi-Fi network configurations. You have full control over each Wi-Fi configuration setting, which enables you to completely replace the native Wi-Fi management application if required. Later in this section you'll get a brief introduction to the APIs used to create, delete, and modify network configurations.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment