android ping网络是否成功

时间:2023-03-08 19:03:37
    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 == ) {
result = true;
}
else {
result = false;
}
}
catch (IOException e) {}
catch (InterruptedException e) {} return result;
}

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