Android 判断是否能真正上网的实例详解
程序员文章站
2023-12-17 18:50:04
android 判断是否能真正上网的实例详解
检测网络是否连接
实现代码:
/**
*
检测网络是否连接
*
*...
android 判断是否能真正上网的实例详解
检测网络是否连接
实现代码:
/** * 检测网络是否连接 * * @return */ private boolean isnetworkavailable() { // 得到网络连接信息 connectivitymanager manager = (connectivitymanager) getsystemservice(context.connectivity_service); // 去进行判断网络是否连接 if (manager.getactivenetworkinfo() != null) { return manager.getactivenetworkinfo().isavailable(); } return false; }
ping的状态
public static final boolean ping() { string result = null; try { string ip = "www.baidu.com";// ping 的地址,可以换成任何一种可靠的外网 process p = runtime.getruntime().exec("ping -c 3 -w 100 " + ip);// ping网址3次 // 读取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.d("------ping-----", "result content : " + stringbuffer.tostring()); // ping的状态 int status = p.waitfor(); if (status == 0) { result = "success"; return true; } else { result = "failed"; } } catch (ioexception e) { result = "ioexception"; } catch (interruptedexception e) { result = "interruptedexception"; } finally { log.d("----result---", "result = " + result); } return false; }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!