Skip to content

Instantly share code, notes, and snippets.

@jcordeiro
Last active August 29, 2015 13:57
Show Gist options
  • Save jcordeiro/9771734 to your computer and use it in GitHub Desktop.
Save jcordeiro/9771734 to your computer and use it in GitHub Desktop.
A method to determine whether or not an Android device has network connectivity
private boolean isNetworkConnected() {
// get Connectivity Manager to get network status
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true; //we have a connection
} else {
return false; // no connection!
}
}
/* Make Sure you add these permissions to your Android manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment