Android执行ping命令检查网络
【摘要】 因为ping要用到网络请求,所以要在AndroidManifest.xml加入网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
1
1、准备好ping命令
将IP或域名和一些参数组合成一条ping命令:
private String spellPing(Stri...
因为ping要用到网络请求,所以要在AndroidManifest.xml加入网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
- 1
1、准备好ping命令
将IP或域名和一些参数组合成一条ping命令:
private String spellPing(String ip) { String countCmd = " -c 4 "; String sizeCmd = " -s 64 "; String timeCmd = " -i 1 "; return "ping" + countCmd + timeCmd + sizeCmd + ip; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2、执行ping命令
执行ping命令就是通过使用Runtime.getRuntime().exec(ping)。
Runtime.getRuntime().exec()可以执行各种各样的命令。
private void executePingCmd(String pingCmd){ executorService = Executors.newSingleThreadExecutor(); executorService.execute(new Thread(new PingTask(pingCmd, pingHandler, 1))); } // 创建ping任务 private class PingTask implements Runnable { private String ping; private PingHandler pingHandler; private long delay; public PingTask(String ping, PingHandler pingHandler, long delay) { this.ping = ping; this.pingHandler = pingHandler; this.delay = delay; } @Override public void run() { Process process = null; BufferedReader successReader = null; BufferedReader errorReader = null; try { // 执行ping命令 process = Runtime.getRuntime().exec(ping); // success successReader = new BufferedReader(new InputStreamReader(process.getInputStream())); // error errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); String lineStr; while ((lineStr = successReader.readLine()) != null) { // receive Message msg = pingHandler.obtainMessage(); msg.obj = lineStr + "\r\n"; msg.what = 10; msg.sendToTarget(); } while ((lineStr = errorReader.readLine()) != null) { // receive Message msg = pingHandler.obtainMessage(); msg.obj = lineStr + "\r\n"; msg.what = 10; msg.sendToTarget(); } Thread.sleep(delay * 1000); } catch (Exception e) { e.printStackTrace(); } finally { try { if (successReader != null) { successReader.close(); } if (errorReader != null) { errorReader.close(); } } catch (IOException e) { e.printStackTrace(); } if (process != null) { process.destroy(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
3、显示执行结果
通过Handler来通知主线程通知更新UI。
private static class PingHandler extends Handler { private WeakReference<MainActivity> weakReference; public PingHandler(MainActivity activity) { this.weakReference = new WeakReference<>(activity); } @Override public void handleMessage(@NonNull Message msg) { switch (msg.what) { case 10: // 3、将结果显示出来 String resultMsg = (String) msg.obj; weakReference.get().pingAdapter.addString(resultMsg); weakReference.get().mRecylerView.scrollToPosition(weakReference.get().pingAdapter.getItemCount() - 1); break; default: break; } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/100134430
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)