看板 AndroidDev 關於我們 聯絡資訊
※ 引述《slemonade (淳)》之銘言: : ※ 引述《slemonade (淳)》之銘言: : : 請問各位前輩,當手機開啟wifi or 3g,可以用ConnectivityManager獲得連線資料;但是當wifi是內網且沒有上網的能力(無法看網頁),獲得的資訊一樣是有連線的;有沒有辦法可以知道wifi是不是確實可以上網的呢? 謝謝 : Process p= Runtime.getRumtime.exec(ping -c 1 www.google.com); p.waitfor(); 用ping如果ping不到的話,會卡住呢,請問有什麼解決方式或其他方法嗎? Try it. public static boolean ping(String url, int timeout) { // Otherwise an exception may be thrown on invalid SSL certificates. url = url.replaceFirst("https", "http"); HttpURLConnection connection = null; try { connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); connection.setRequestMethod("HEAD"); int responseCode = connection.getResponseCode(); return (200 <= responseCode && responseCode <= 399); } catch (IOException exception) { return false; } finally { if (connection != null) { connection.disconnect(); } } } 至於推文提到的 walled garden 問題 http://goo.gl/dfWq7A 這個link可以看看 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.129.214.149 ※ 文章網址: http://www.ptt.cc/bbs/AndroidDev/M.1401123251.A.3FC.html
johnpage:ping 沒使用timeout 05/27 04:48
slemonade:感謝大大們的方法,雖然看不太懂,不過還是試試^^ 05/28 00:07