Android how to check wifi is connected but no internet connection

androidandroid-wifi

android my device connected with wifi but how to if wifi is connected but these is no internet connection

following is my code that i trying to check if no internet connection

public static boolean isConnectedWifi(Context context) {
        NetworkInfo info=null;
        if(context!=null){
            info= IsNetConnectionAvailable.getNetworkInfo(context);
        }
        return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
    }

it always return true when no internet access

Best Answer

NetworInfo.isAvailable and NetworkInfo.isConnected only indicate whether network connectivity is possible or existed, they can't indicate whether the connected situation has access to the public internet, long story short, they can't tell us the device is online indeed.

To check whether a device is online, try the following methods:

First:

@TargetApi(Build.VERSION_CODES.M)
public static boolean isNetworkOnline1(Context context) {
    boolean isOnline = false;
    try {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkCapabilities capabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());  // need ACCESS_NETWORK_STATE permission
        isOnline = capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return isOnline;
}

Strength: 1. could run on UI thread; 2. fast and accurate.

Weakness: need API >= 23 and compatibility issues.

Second:

public static boolean isNetworkOnline2() {
    boolean isOnline = false;
    try {
        Runtime runtime = Runtime.getRuntime();
        Process p = runtime.exec("ping -c 1 8.8.8.8");
        int waitFor = p.waitFor();
        isOnline = waitFor == 0;    // only when the waitFor value is zero, the network is online indeed

        // BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        // String str;
        // while ((str = br.readLine()) != null) {
        //     System.out.println(str);     // you can get the ping detail info from Process.getInputStream()
        // }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return isOnline;
}

Strength: 1. could run on UI thread; 2. you can ping many times and do statistics for min/avg/max delayed time and packet loss rate.

Weakness: compatibility issues.

Third:

public static boolean isNetworkOnline3() {
    boolean isOnline = false;
    try {
        URL url = new URL("http://www.google.com"); // or your server address
        // URL url = new URL("http://www.baidu.com");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Connection", "close");
        conn.setConnectTimeout(3000);
        isOnline = conn.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return isOnline;
}

Strength: could use on all devices and APIs.

Weakness: time-consuming operation, can't run on UI thread.

Fourth:

public static boolean isNetworkOnline4() {
    boolean isOnline = false;
    try {
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("8.8.8.8", 53), 3000);
        // socket.connect(new InetSocketAddress("114.114.114.114", 53), 3000);
        isOnline = true;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return isOnline;
}

Strength: 1. could use on all devices and APIs; 2. relatively fast and accurate.

Weakness: time-consuming operation, can't run on UI thread.