Android 用ping的方法判断当前网络是否可用
判断网络的情况中,有个比较麻烦的情况就是连上了某个网络,但是那个网络无法上网 ,,, = =
想到了用ping指令来判断,经测试,可行~ ~ ~
private static final boolean ping() {
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 Traffic] 让android应用在传输网络数据的时候更省电
- Android Studio中获取SHA1或MD5的方法
- android中ToggleButton的使用
- Android中的DigitalClock和AnalogClock
- android代码格式化方法小结
- android脚步---UI界面修改,关于activity中增加按钮和监听
- Android:轮播图ViewPager自动轮播最简单方法
- Atitit 常用技能点体系树 os win linux android 前后端 gui h5 vue js jquery bootstrap cocos2d Jafavx wpf
- Android P适配以太网功能开发指南
- Rockchip Android平台GPU/DDR负载变频阈值调整方法
- Android 12.0 屏蔽FallbackHome机制去掉android正在启动直接进入默认Launcher功能实现
- Android 进入其他app和退出App杀死进程的方法详解
- 【Android 性能优化】应用启动优化 ( 启动优化项目 | 界面启动时间 | 启动优化项目 | 方法追踪 MethodTracing )
- Android 系统休眠唤醒 android-suspend