您的位置:首页 > 理论基础 > 计算机网络

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

2015-10-30 14:31 1006 查看

/**

* 用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;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: