我使用以下代码调用android设备ping命令:
public static String pingServer() {
PingResult result = new PingResult();
String jsonString = null;
try {
String command = "ping -c 3 192.168.8.185";
Process p = Runtime.getRuntime().exec(command);
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);
buffer.append("\n");
}
String bufferStr = buffer.toString();
System.out.println(bufferStr);
} catch (Exception e) {
System.out.println("---------------exception-----------ping");
System.out.println(e.getMessage());
// e.printStackTrace();
}
}
BufferStr始终获取最后一行数据。
发送3个包,3个接收,0%丢包,时间2000ms详细结果可以得到。
答案 0 :(得分:1)
使用InetAddress.isReachable可能更简单,更容易:
if (InetAddress.getByName("192.168.8.185").isReachable(6000))
...