Android to determine if you can actually surf the internet

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.