Android to determine if you can actually surf the internet

Source: Internet
Author: User

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

Related Article

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.