Android 用代码Ping网络
生活随笔
收集整理的這篇文章主要介紹了
Android 用代码Ping网络
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Android 用代碼Ping網絡
protected void runLogCatCommand(String command) {String cmd = "ping -c 4 " + command; //ping command(例:www.baidu.com) 4次try {logcatProcess = Runtime.getRuntime().exec(cmd);printLogCatInfo(logcatProcess);} catch (Exception e) {e.printStackTrace();}}private Process logcatProcess;private Thread logcatExecThread;private boolean runLogcatThread = false;private void printLogCatInfo(Process process) {final BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));final BufferedReader errInput = new BufferedReader(new InputStreamReader(process.getErrorStream()));runLogcatThread = true;logcatExecThread = new Thread() {@Overridepublic void run() {String tmp1 = null;String tmp2 = null;try {while (((tmp1 = input.readLine()) != null || (tmp2 = errInput.readLine()) != null)&& runLogcatThread) {if (tmp1 != null) {Log.d("Ping", "tmp1 = " + tmp1);}if (tmp2 != null) {Log.d("Ping", "tmp2 = " + tmp2);}}input.close();errInput.close();} catch (Exception e) {e.printStackTrace();}runLogcatThread = false;}};logcatExecThread.start();}權限
android:sharedUserId=“android.uid.system”
uses-permission android:name=“android.permission.INTERNET” />
uses-permission android:name=“android.permission.ACCESS_NETWORK_STATE” />
如果失敗時出現 tmp2 ping: icmp open socket: Operation not permitted 這個日志檢查一下system/bin/ping的權限 可以試著修改成 04775 即 -rwsrwxr-x
總結
以上是生活随笔為你收集整理的Android 用代码Ping网络的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件测试中什么是正交分析法,软件测试用例
- 下一篇: android sina oauth2.