Android 检测网络是否真的有效isNetworkConnected() 和ping()
2017-12-26 10:59
471 查看
1.isNetworkConnected()
2.ping这种方式我在adb shell ping www.baidu.com是可以的。但是在测试demo里面不知道是不是权限问题不行。因为我是在framework上用的。编译了版本发现是可以的。如果有朋友知道为什么单独写个demo不行的原因求告知
private boolean isNetworkConnected() { final ConnectivityManager connManager = (ConnectivityManager) mContext .getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo netInfo = connManager.getActiveNetworkInfo(); if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) { return true; } return false; }
2.ping这种方式我在adb shell ping www.baidu.com是可以的。但是在测试demo里面不知道是不是权限问题不行。因为我是在framework上用的。编译了版本发现是可以的。如果有朋友知道为什么单独写个demo不行的原因求告知
public static boolean pingHostnamePass(String host) { boolean pass = false; if (host == null) { return false; } try { // TODO: Hardcoded for now, make it UI configurable pass = false; String command = "ping -c 5 -w 10 " + host; Process p = Runtime.getRuntime().exec(command); int status = p.waitFor(); if (status == 0) { pass = true; } else { pass = false; } } catch (UnknownHostException e) { Log.e(TAG, "UnknownHostException: " + e.toString()); } catch (IOException e) { Log.e(TAG, "IOException: " + e.toString()); } catch (InterruptedException e) { Log.e(TAG, "InterruptedException: " + e.toString()); } Log.d(TAG, "====> Access " + host + " pass? " + pass); return pass; }
相关文章推荐
- 使用API判断网络是否连通(InternetGetConnectedState / IsNetworkAlive)
- 使用API判断网络是否连通(InternetGetConnectedState / IsNetworkAlive)
- 使用API判断网络是否连通(InternetGetConnectedState / IsNetworkAlive)
- 使用API判断网络是否连通(InternetGetConnectedState / IsNetworkAlive)
- 使用API判断网络是否连通(InternetGetConnectedState / IsNetworkAlive)
- 使用API判断网络是否连通(InternetGetConnectedState / IsNetworkAlive)
- 使用API判断网络是否连通(InternetGetConnectedState / IsNetworkAlive)
- android 判断当前网络是否可用(ping网络),包含wifi和移动数据网络
- android ping网络是否成功
- [Android实例] android 检测网络是否存在 [复制链接]
- Android检测网络是否正常代码!
- 检测android某个intent是否有效
- android检测当前网络是否可用
- Android 检测当前网络是否可用
- Android检测设备是否连上网络
- C#使用HTTP头检测网络资源是否有效
- 开启Android程序时检测网络与GPS是否打开
- android 判断网络连接、sim卡信息以及ping操作是否网络连接正常
- 收藏一个Ping的小工具类,可用于网络中检测目的设备是否在线
- Android检测手机是否有网络连接可用