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;
}
相关文章推荐
- 问题描述: android.os.NetworkOnMainThreadException异常(不要在主线程访问网络资源)
- swift HTTP通讯
- 关于Cocos C++ http 有中文参数 服务器接收乱码问题解决!
- TCP校验和算法
- 禁用IOS9 https
- httpClient发送请求
- Java网络编程--echo服务器
- 网络通信基础
- http长连接和短连接
- Linux(Red Hat6.5)下安装svn服务器,并通过http访问
- java 从网络Url中下载文件
- TCP/IP详解 卷1 笔记 第5章 RARP
- Linux tcpdump命令详解
- 操作系统与网络实现 之二
- Cocos2d使用HttpClient
- 手机如何使用USB数据线共享PC网络
- 《计算机网络-自顶向下方法》读书笔记-网络层篇
- IIS7中配置集成模式,使其可使用httpModules、httpHandlers【转】
- Java进行http请求工具类代码(支持https)
- TCP/IP详解 卷1 笔记 第4章