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 ;}