Skip to content

Latest commit

 

History

History
23 lines (16 loc) · 814 Bytes

android-check-internet-connection.md

File metadata and controls

23 lines (16 loc) · 814 Bytes

title: Android: Check for an internet connection tags: android,android-connectivity

Stick this in your Application class:

public static App sApp;

@Override public void onCreate() {
  super.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.