Check if device has internet connectivity

suggest change

getAllNetworkInfo() needs ACCESS_NETWORK_STATE. A connected interface means the device has a network. It does not prove a remote host answered.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
/**
 * @return true if any network interface is in the connected state
 */
public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Log.d("NetworkCheck", "isNetworkAvailable: No");
        return false;
    }

    // Every data interface (Wi-Fi, mobile, and so on).
    NetworkInfo[] info = connectivity.getAllNetworkInfo();
    if (info != null) {
        for (int i = 0; i < info.length; i++) {
            if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                Log.d("NetworkCheck", "isNetworkAvailable: Yes");
                return true;
            }
        }
    }
    return false;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents