android 網路連線了wifi,但是並沒有網路;或者手機聯網了 ,APP檢測顯示未聯網 阿新 • • 發佈:2019-01-01 /** * 用Ping的方法檢測網路可行性 * @return */ public static final boolean pingIsInternetConnect() { String result = null; try { String ip = "www.baidu.com";// 除非百度掛了,否則用這個應該沒問題~ Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);//ping3次 // 讀取ping的內容,可不加。 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.i("TTT", "result content : " + stringBuffer.toString()); // PING的狀態 int status = p.waitFor(); if (status == 0) { result = "successful~"; return true; } else { result = "failed~ cannot reach the IP address"; } } catch (IOException e) { result = "failed~ IOException"; } catch (InterruptedException e) { result = "failed~ InterruptedException"; } finally { Log.i("TTT", "result = " + result); } return false; }