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.
*@author Suncat*@category Determine if there is an extranet connection (the normal method can not determine whether the network connection, such as connecting the LAN)* @return*/ Public Static Final BooleanPing () {String result=NULL; Try{String IP= "www.baidu.com";//Ping address, can be replaced by any kind of reliable external networkProcess p = runtime.getruntime (). EXEC ("ping-c 3-w" + IP);//ping url 3 times//reads the ping content, can not addInputStream input =P.getinputstream (); BufferedReader in=NewBufferedReader (NewInputStreamReader (input)); StringBuffer StringBuffer=NewStringBuffer (); String content= ""; while(content = In.readline ())! =NULL) {stringbuffer.append (content); } log.d ("------Ping-----", "Result content:" +stringbuffer.tostring ()); //Status of Ping intStatus =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 you can actually surf the internet