本人写了一个Android的demoAPP,为了在手机上运行ping命令,代码如下:
private boolean pingHostname(String ipAddress) {
Runtime runtime = Runtime.getRuntime();
// Process process = runtime.exec("/system/bin/ping -c 1 " + ipAddress);
String line = null; // 返回行信息
BufferedReader br = null;
boolean res = false;// 结果
try {
Process process = runtime.exec("ping " + ipAddress + " -n 1 -w 3000"); // PING
br = new BufferedReader(new InputStreamReader(process.getInputStream()));// 从字节中读取文本
while ((line = br.readLine()) != null) {
if (line.contains("TTL")) {
res = true;
break;
}
}
process.waitFor();
br.close();
if (res) {
System.out.println("ping 通 ...");
} else {
System.out.println("ping 不通...");
}
} catch (IOException e) {
System.out.println(e);
runtime.exit(1);
} catch (InterruptedException e1) {
System.out.println(e1);
}
return res;
}
而实际结果是line = br.readLine()这里返回的总是null,导致ping结果总是失败。
请高手帮忙指点,如何在APP里实现ping