Skip to content

Instantly share code, notes, and snippets.

@VarunBarad
Last active February 15, 2018 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VarunBarad/f9bde6f004a560b54375d27ad26911c0 to your computer and use it in GitHub Desktop.
Save VarunBarad/f9bde6f004a560b54375d27ad26911c0 to your computer and use it in GitHub Desktop.
Checking network connectivity on Android
public class ConnectivityHelper {
public static boolean isConnectedToNetwork(Context context) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isConnected = false;
if (connectivityManager != null) {
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
isConnected = (activeNetwork != null) && (activeNetwork.isConnectedOrConnecting());
}
return isConnected;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment