Judging the situation of the network, there is a more troublesome situation is connected to a network, but that network can not access the internet,, = =
Think of the ping command to judge, tested, feasible ~ ~ ~
Private Staticfinal Boolean ping () {
String result = null;
try {
String IP = "www.baidu.com";//unless Baidu hangs, otherwise use this should have no problem ~
Process p = runtime.getruntime (). EXEC ("ping-c 3-w" + IP);//ping3 times
Read the contents of the ping without adding.
InputStream input = P.getinputstream ();
BufferedReader in = new BufferedReader (new InputStreamReader (input));
StringBuffer StringBuffer = new StringBuffer ();
String content = "";
while (content = In.readline ())!=null) {
Stringbuffer.append (content);
}
LOG.I ("TTT", "Result content:" + stringbuffer.tostring ());
Status of Ping
int status = P.waitfor ();
if (status = = 0) {
result = "successful~";
Returntrue;
} else {
result = "failed~ cannot reach the IP address";
}
} catch (IOException e) {
result = "failed~ IOException";
} catch (Interruptedexception e) {
result = "failed~ interruptedexception";
} finally {
LOG.I ("TTT", "result =" + result);
}
Returnfalse;
}