Skip to content

Instantly share code, notes, and snippets.

@mertsimsek
Last active December 15, 2015 00:58
Show Gist options
  • Save mertsimsek/5176167 to your computer and use it in GitHub Desktop.
Save mertsimsek/5176167 to your computer and use it in GitHub Desktop.
Looks for internet availability on Android.
public class InternetAvailability{
public boolean isInternetAvailable(){
try {
ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
nInfo.getActiveNetworkInfo().isConnectedOrConnecting();
Log.i(TAG, "Net avail:"
+ nInfo.getActiveNetworkInfo().isConnectedOrConnecting());
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
Log.i(TAG, "Network is available");
return true;
} else {
Log.i(TAG, "Network is not available");
return false;
}
} catch (Exception e) {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment