class PingWorker extends Thread {
private final Process process;
private Integer exit;
public PingWorker(Process process) {
this.process = process;
}
@Override
public void run() {
try {
exit = process.waitFor();
} catch (InterruptedException e) {
return;
}
}
}
public int pingCommand(final long timeout) throws IOException, InterruptedException, TimeoutException {
Process process = Runtime.getRuntime().exec("ping -c 1 -w 10" + " " + "www.hao123.com");
PingWorker worker = new PingWorker(process);
worker.start();
try {
worker.join(timeout);
if (worker.exit != null) {
Log.d("111√", "worker.exit=" + worker.exit);
return worker.exit;
} else {
throw new TimeoutException();
}
} catch (InterruptedException ex) {
worker.interrupt();
Thread.currentThread().interrupt();
handler.sendEmptyMessage(3);
throw ex;
} finally {
process.destroy();
}
}