Skip to content

Instantly share code, notes, and snippets.

@eufelipe
Last active July 24, 2017 18:14
Show Gist options
  • Save eufelipe/1794bcd16ea379c9c85854abb4dcbedc to your computer and use it in GitHub Desktop.
Save eufelipe/1794bcd16ea379c9c85854abb4dcbedc to your computer and use it in GitHub Desktop.
/**
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
*/
public static boolean isNetworkAvailable(Context context) {
boolean isConnected = false;
try {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
} catch (Exception e) {
e.printStackTrace();
}
return isConnected;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment