Android: Check for an internet connection

Stick this in your Application class:

public static App sApp;

@Override public void onCreate() {
  sApp = this;

public static boolean isNetworkAvailable() {
  ConnectivityManager cm = (ConnectivityManager) sApp.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo netInfo = cm.getActiveNetworkInfo();
  return netInfo != null && netInfo.isConnectedOrConnecting();

You also need this permission:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

This does not, however, check the internet connection is good: i.e. it may not be able to connect to anything. For that, you'd have to check a connection to another site.

android android-connectivity

Edit on github
comments powered by Disqus
Click me