Skip to content

Instantly share code, notes, and snippets.

@pkavoo
Last active April 2, 2020 21:38
Show Gist options
  • Save pkavoo/d2cab3bdf44217699a0d1ed52f4b488a to your computer and use it in GitHub Desktop.
Save pkavoo/d2cab3bdf44217699a0d1ed52f4b488a to your computer and use it in GitHub Desktop.
Method for checking if internet is turned on
public static boolean isInternetConnected(Context ctx) {
ConnectivityManager connectivityMgr = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// Check if wifi or mobile network is available or not. If any of them is
// available or connected then it will return true, otherwise false;
if (wifi != null) {
if (wifi.isConnected()) {
return true;
}
}
if (mobile != null) {
if (mobile.isConnected()) {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment