Android determines whether there is an Internet connection. android determines

Source: Internet
Author: User

Android determines whether there is an Internet connection. android determines

Sometimes we connect to a Wi-Fi or wired network without an Internet connection. Currently, the Android SDK cannot identify this situation. The general solution is to ping An Internet network.


Solution:


/*** @ Author suncat * @ category determine whether there is an Internet connection (the common method cannot determine whether the Internet is connected, such as connecting to a LAN) * @ return */public static final boolean ping () {String result = null; try {String ip = "www.baidu.com"; // ping address, you can replace it with any reliable Internet Process p = runtime.getruntime(cmd.exe c ("ping-c 3-w 100" + ip); // ping the web site three times // read the ping content, inputStream input = p. getInputStream (); BufferedReader in = new BufferedReader (new InputStreamReader (inpu T); StringBuffer stringBuffer = new StringBuffer (); String content = ""; while (content = in. readLine ())! = Null) {stringBuffer. append (content);} Log. d ("------ ping -----", "result content:" + stringBuffer. toString (); // ping status 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 ;}


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.