判断有没有联网

网友投稿 818 2022-11-25

判断有没有联网

判断有没有联网

/** * 阿里公网: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; }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:几个工具类 web
下一篇:安卓调用webservice两个工具类
相关文章

 发表评论

暂时没有评论,来抢沙发吧~