Skip to content

Instantly share code, notes, and snippets.

@davidllorca
Last active September 16, 2015 10:48
Show Gist options
  • Save davidllorca/29c1de1ae889aa1838b1 to your computer and use it in GitHub Desktop.
Save davidllorca/29c1de1ae889aa1838b1 to your computer and use it in GitHub Desktop.
Check network connetion
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
...
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
// fetch data
} else {
// display error
}
...
// requires ACCES_NETWORK permission
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment