0
点赞
收藏
分享

微信扫一扫

判断有没有联网

/**
* 阿里公网:223.5.5.5
*
* @return 判断单个应用是个可以联网, 原理:ping网络
*/
public static boolean isNetworkOnline() {

Runtime runtime = Runtime.getRuntime();
Process ipProcess = null;
try {
ipProcess = runtime.exec("ping -c 5 -w 4 223.5.5.5");
InputStream input = ipProcess.getInputStream();

BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer stringBuffer = new StringBuffer();
String content = "";
while ((content = in.readLine()) != null) {
stringBuffer.append(content);
}

int exitValue = ipProcess.waitFor();
if (exitValue == 0) {
//WiFi连接,网络正常
return true;
} else {
if (stringBuffer.indexOf("100% packet loss") != -1) {
XLog.showArgsInfo("网络丢包严重,判断为网络未连接");
return false;
} else {
XLog.showArgsInfo("网络未丢包,判断为网络连接");
return true;
}
}
} catch (IOException | InterruptedException e) {
XLog.printExceptionInfo(e);
} finally {
if (ipProcess != null) {
ipProcess.destroy();
}
runtime.gc();
}
return false;
}


举报

相关推荐

0 条评论