Android - Network (Connectivity Manager)

> Android

1 - About

The ConnectivityManager answers queries about the state of network connectivity. It also notifies applications when network connectivity changes.

3 - Implementation

3.1 - Permission

ACCESS_NETWORK_STATE

In the manifest, add the permission:

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

3.2 - Snippet

/**
 * Returns true if the network is available or about to become available.
 *
 * @param c Context used to get the ConnectivityManager
 * @return
 */
static public boolean isNetworkAvailable(Context c) {
	ConnectivityManager cm =
			(ConnectivityManager)c.getSystemService(Context.CONNECTIVITY_SERVICE);
 
	NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
	return activeNetwork != null &&
			activeNetwork.isConnectedOrConnecting();
}

4 - Documentation / Reference