Android to determine whether you can surf the internet, commonly used is the following method:
/** * Detects if the network is connected * * @return */private boolean isnetworkavailable () {//Get network connection information Connectivitymanager Manager = (connectiv Itymanager) Getsystemservice (context.connectivity_service);//Go to determine if the network is connected if (Manager.getactivenetworkinfo ()! = NULL ) {return Manager.getactivenetworkinfo (). isavailable ();} return false;}
However, sometimes we connect to a network that does not have an extranet connection or need to enter an account and password to link to an extranet, which can occur although the network is available, but the extranet is not accessible. In response to this situation, the general solution is to ping an external network, if you can ping the instructions can really surf the internet, the code is as follows
* @author sichard * @category Determine if there is an extranet connection (the normal method can not determine whether the network is connected to the outside network, For example, connect to a local area network) * @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 external network process p = runtime.getruntime (). EXEC ("ping -c 3 -w 100 " + ip);// Ping URL 3 times Read the contents of Ping, can not add &NBSp; 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.D ("------Ping-----", "result content : " + stringbuffer.tostring ()); // Status 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"; &nbsP;} finally {   LOG.D ("----Result---", "result = " + result); } return false;
Android to determine if you can actually surf the internet