Android - Network (Connectivity Manager)

Card Puncher Data Processing

About

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

Implementation

Permission

ACCESS_NETWORK_STATE

In the manifest, add the permission:

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

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();
}

Documentation / Reference







Share this page:
Follow us:
Task Runner