注意要添加以下权限
<uses-permission android:name="android.permission.INTERNET"/>
public static String pingHost(String str){ String resault=""; try { // TODO: Hardcoded for now, make it UI configurable Process p = Runtime.getRuntime().exec("/system/bin/ping -c 4 -w 100 " +str); //Process p = Runtime.getRuntime().exec("ping 192.168.1.111"); int status = p.waitFor(); InputStream input = p.getInputStream(); //Log.i("info"); BufferedReader in = new BufferedReader(new InputStreamReader(input)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); Log.i("info", line); } if (status == 0) { // mTextView.setText("success") ; resault="success"; Log.i("info", "success......"); } else { resault="faild"; // mTextView.setText("fail"); Log.i("info", "failed......" + status + "..."); } } catch (IOException e) { // mTextView.setText("Fail: IOException"+"\n"); } catch (InterruptedException e) { // mTextView.setText("Fail: InterruptedException"+"\n"); } return resault; }