Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save deanpanayotov/9344170 to your computer and use it in GitHub Desktop.
Save deanpanayotov/9344170 to your computer and use it in GitHub Desktop.
public static boolean isNetworkAvailable(Activity activity) {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

Note that having an active network interface doesn't guarantee that a particular networked service is available. Networks issues, server downtime, low signal, captive portals, content filters and the like can all prevent your app from reaching a server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment