android 网络连接了wifi,但是并没有网络;或者手机联网了 ,APP检测显示未联网

/**
* 用Ping的方法检测网络可行性
* @return
*/
public static final boolean pingIsInternetConnect() {
String result = null;
try {
String ip = "www.baidu.com";// 除非百度挂了,否则用这个应该没问题~
Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);//ping3次
// 读取ping的内容,可不加。
InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer stringBuffer = new StringBuffer();
String content = "";
while ((content = in.readLine()) != null) {
stringBuffer.append(content);
}
Log.i("TTT", "result content : " + stringBuffer.toString());
// PING的状态
int status = p.waitFor();
if (status == 0) {
result = "successful~";
return true;
} else {
result = "failed~ cannot reach the IP address";
}
} catch (IOException e) {
result = "failed~ IOException";
} catch (InterruptedException e) {
result = "failed~ InterruptedException";
} finally {
Log.i("TTT", "result = " + result);
}
return false;
}


你可能感兴趣的:(android逻辑方法)