About
The ConnectivityManager answers queries about the state of network connectivity. It also notifies applications when network connectivity changes.
Articles Related
Implementation
Permission
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();
}