android ping网络是否成功

    public static boolean pingHost(String str) {  //str  为要ping的IP地址
        boolean result = false;
        try {
            Process p = Runtime.getRuntime().exec("ping -c 1 -w 100 " + str);
            int status = p.waitFor();
            if (status == 0) {
                result = true;
            }
            else {
                result = false;
            }
        }
        catch (IOException e) {}
        catch (InterruptedException e) {}

        return result;
    }

虽然可以用ConnectivityManager 来获取各类网络的状态,但是只能获取网络是否连接,并不能判断wifi或其他网络能否成功连接特定的网络; 以上的方法直接ping IP就可以确保当前网络可以访问

原文地址:https://www.cnblogs.com/mushishi/p/3425724.html