Android ping命令 -- Runtime

來源:互聯網
上載者:User

標籤:

代碼:

 1     public String Run(String _strsIp) throws Exception 2     { 3         String strRst = ""; 4         try 5         { 6             String str = "ping -c 1 "+_strsIp; 7             Runtime runtime = Runtime.getRuntime(); 8             Process process = runtime.exec(str); 9 10             //等待進程執行完畢11             if(process.waitFor() != 0)12             {13                 //如果進程運行結果不為0,表示進程是錯誤退出的14                 //獲得進程執行個體的錯誤輸出15                 InputStream streamErr = process.getErrorStream();16                 //do something17                 18                 strRst = InputStream2String(streamErr);19                 if (streamErr != null)20                     streamErr.close();21                 22                 if (strRst.isEmpty())23                 {24                     InputStream streamIn = process.getInputStream();25                     strRst = InputStream2String(streamIn);26                     if (streamIn != null)27                         streamIn.close();28                     29                     if (strRst.isEmpty())30                         strRst = "Failed : ping failed . ";31                 }32                 return strRst;33             }34             35             InputStream streamIn = process.getInputStream();36             strRst = InputStream2String(streamIn);37             if (streamIn != null)38                 streamIn.close();39             40             if (strRst.isEmpty())41                 strRst = "Succeed : ping failed . ";42         }43         catch (Exception ex)44         {45             //ex.printStackTrace();46             StringWriter sw = new StringWriter();47             PrintWriter pw = new PrintWriter(sw);48             ex.printStackTrace(pw);49             strRst = "Err : "+sw.toString();50         }51         return strRst;52     }

 

Android ping命令 -- Runtime

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.