qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Android:Ping命令測試網絡

          import java.io.BufferedReader;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.InputStreamReader;
          import android.app.Activity;
          import android.os.AsyncTask;
          import android.os.Bundle;
          import android.util.Log;
          public class MainActivity extends Activity {
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          new NetPing().execute();
          }
          public String Ping(String str) {
          String resault = "";
          Process p;
          try {
          //ping -c 3 -w 100  中  ,-c 是指ping的次數 3是指ping 3次 ,-w 100  以秒為單位指定超時間隔,是指超時時間為100秒
          p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + str);
          int status = p.waitFor();
          InputStream input = p.getInputStream();
          BufferedReader in = new BufferedReader(new InputStreamReader(input));
          StringBuffer buffer = new StringBuffer();
          String line = "";
          while ((line = in.readLine()) != null){
          buffer.append(line);
          }
          System.out.println("Return ============" + buffer.toString());
          if (status == 0) {
          resault = "success";
          } else {
          resault = "faild";
          }
          } catch (IOException e) {
          e.printStackTrace();
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          return resault;
          }
          private class NetPing extends AsyncTask<String, String, String> {
          @Override
          protected String doInBackground(String... params) {
          String s = "";
          s = Ping("www.baidu.com");
          Log.i("ping", s);
          return s;
          }
          }
          }
           !??!別忘了加訪問網絡權限?。。?/div>
            下面是運行截圖:
            如果對 運行結果中的  PING www.a.shifen.com (61.135.169.125) 感到懷疑,那么可以試一下 61.135.169.125 就是百度的網址。
            suceess則表示連接成功
            如果當前沒有連接網絡,return 會是 空的 ,結果會是 faild。
            如果ping的網址是不存在的,而且還連接著網絡,那么程序就會 停在
            int status = p.waitFor();
            這里,不再向下運行。
            Ping遠程IP    
            這一命令可以檢測本機能否正常訪問Internet。比如本地電信運營商的IP地址為:202.101.224.69。在MS-DOS方式下執行命令:Ping 202.101.224.69,如果屏幕顯示:
          C:\Documents and Settings\Administrator>ping 202.101.224.69
          Pinging 202.101.224.69 with 32 bytes of data:
          Reply from 202.101.224.69: bytes=32 time=2ms TTL=250
          Reply from 202.101.224.69: bytes=32 time=2ms TTL=250
          Reply from 202.101.224.69: bytes=32 time=3ms TTL=250
          Reply from 202.101.224.69: bytes=32 time=2ms TTL=250
          Ping statistics for 202.101.224.69:
          Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),  Approximate round trip times in milli-seconds:
          Minimum = 2ms, Maximum = 3ms, Average = 2ms
            則表明運行正常,能夠正常接入互聯網。反之,則表明主機文件(windows/host)存在問題。

          posted on 2014-03-12 10:40 順其自然EVO 閱讀(8377) 評論(0)  編輯  收藏 所屬分類: android

          <2014年3月>
          2324252627281
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          導航

          統計

          • 隨筆 - 3936
          • 文章 - 404
          • 評論 - 179
          • 引用 - 0

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 友谊县| 肇州县| 二连浩特市| 沛县| 洛隆县| 黄石市| 博白县| 湘西| 宁津县| 自治县| 莫力| 漠河县| 方山县| 海安县| 新民市| 长岭县| 敦化市| 三江| 泸水县| 青神县| 潮州市| 资溪县| 西林县| 泰州市| 讷河市| 六盘水市| 沐川县| 石台县| 峨山| 湛江市| 卓资县| 望谟县| 东阳市| 萨嘎县| 光泽县| 衡南县| 德阳市| 香港 | 南靖县| 北宁市| 获嘉县|