PingUtil in Android
2016-03-17 19:31
267 查看
Ping a host in Android:
“ping -c 1 127.0.0.1”
-c 1: The ping times.
Ping codes is simple, but:
1, you must run it in background thread since Process.waitfor() will blocks current thread.
2, And you should control the the execution time for Process.waitfor().
The solutions of above 2 issues could be:
1, AsyncTask to run Process.waitfor() in background;
2, Handler.sendEmptyMessageDelayed() to send a delayed message which cancel the AsyncTask if the background codes running too long time.
Codes in Github.
“ping -c 1 127.0.0.1”
-c 1: The ping times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | boolean flag = false; Runtime run = Runtime.getRuntime(); Process proc = null; try { proc = run.exec("ping -c 1 127.0.0.1"); int result = proc.waitFor(); if (result == 0) { flag = true; } else { flag = false; } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { proc.destroy(); } |
1, you must run it in background thread since Process.waitfor() will blocks current thread.
2, And you should control the the execution time for Process.waitfor().
The solutions of above 2 issues could be:
1, AsyncTask to run Process.waitfor() in background;
2, Handler.sendEmptyMessageDelayed() to send a delayed message which cancel the AsyncTask if the background codes running too long time.
Codes in Github.
相关文章推荐
- Android客户端的图形化拖放操作的设计实现
- LED notification in Android device
- Android实现播放GIF动画的强大ImageView
- android Style(样式)的解析
- android 内存处理工具
- android 5.X Toolbar+DrawerLayout实现抽屉菜单
- 【Android】动画 —— view高度渐变的动画
- 读书笔记-Android初学笔记
- Android API level 版本对应关系
- Android新手入门2016(9)--ListView之SimpleAdapter和SimpleCursorAdapter
- Android音频底层调试-基于tinyalsa
- Android分享笔记(5) Android 与 JS 交互
- Android进程管理
- android:showAsAction="never"报错
- Android javax.net.ssl.SSLException: hostname in certificate didn't match...
- android应用程序如何调用支付宝接口
- Android开发--List与ArrayList区别
- 【转】android笔记--保存和恢复activity的状态数据
- 【转】Android 组件系列-----Activity保存状态
- Android事件处理