Ping a host in Android:
“ping -c 1 127.0.0.1”
-c 1: The ping times.
1 |
boolean flag = false; |
Ping codes is simple, but:
1, you must run it in background thread since Process.waitfor() will blocks current thread.
2, And you should control the the execution time for Process.waitfor().
The solutions of above 2 issues could be:
1, AsyncTask to run Process.waitfor() in background;
2, Handler.sendEmptyMessageDelayed() to send a delayed message which cancel the AsyncTask if the background codes running too long time.