Last active
September 16, 2015 10:48
-
-
Save davidllorca/29c1de1ae889aa1838b1 to your computer and use it in GitHub Desktop.
Check network connetion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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