Sometimes we connect to a Wi-Fi with no extranet connection or wired to this extreme situation, the current Android SDK does not recognize this situation, the general solution is to ping an external network.
Scheme:
/** * @author Suncat * @category Determine if there is an extranet connection (the normal method cannot determine if the network connection is connected, such as a LAN connection) * @return */public static final Boolean ping () { String result = null; try {String IP = "www.baidu.com";//ping address can be replaced by any kind of reliable extranet Process P = runtime.getruntime () . EXEC ("ping-c 3-w" + IP),//Ping URL 3 times//Read ping content, can not add inputstream input = P.getinput Stream (); BufferedReader in = new BufferedReader (new InputStreamReader (input)); StringBuffer StringBuffer = new StringBuffer (); String content = ""; while (content = In.readline ()) = null) {stringbuffer.append (content); } log.d ("------ping-----", "Result content:" + stringbuffer.tostring ()); The state of ping int status = P.waitfor (); if (status = = 0) {result = "success"; return true; } else {result = "failed"; }} catch (IOException e) {result = "IOException"; } catch (Interruptedexception e) {result = "interruptedexception"; } finally {LOG.D ("----Result---", "result =" + result); } return false;}
Android to determine if there is an external network connection