Q: Detect whether there is an Internet connection available on Android [duplicate]

D: I need to detect whether the Android device is connected to the Internet.

Test Case #16


File ID: #4239019-5-cc


public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
                             .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo ! = null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}

  1. This does not check if the phone is connected to the internet. Only that a network connection has been made.
  2. If you are creating a `Utils` method i.e. not accessing this from where `Context` reference is available, you'll need to pass `Context` as an argument.
  3. To be safe I would `return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();`

Comments Quality
Accurate?:
Precise?:
Concise?:
Useful?: