* @author sichard * @category 判斷是否有外網鏈接(普通方法不能判斷外網的網絡是否鏈接,好比鏈接上局域網) * @return */ public static final boolean ping() { String result = null ; try { String ip = "www.baidu.com" ; // ping 的地址,能夠換成任何一種可靠的外網 Process p = Runtime.getRuntime().exec( "ping -c 3 -w 100 " + ip); // ping網址3次 // 讀取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.d( "------ping-----" , "result content : " + stringBuffer.toString()); // ping的狀態 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 ; |