Skip to content

Instantly share code, notes, and snippets.

@lucamtudor
Created July 2, 2013 10:00
Show Gist options
  • Save lucamtudor/5908110 to your computer and use it in GitHub Desktop.
Save lucamtudor/5908110 to your computer and use it in GitHub Desktop.
Method to check if the Android device is connected to the Internet.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public static boolean hasInternetAccess() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
return false;
} else {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment