1 /** 2 * 判断本机当前的网络状态是否联通 3 * 在这里主要用到中国天气信息,所以访问百度地址是否能够访问成功来判断当前的网络状态 4 */ 5 public static boolean isConnect() { 6 Runtime runtime = Runtime.getRuntime(); 7 try { 8 Process process = runtime.exec("ping " + "www.baidu.com"); 9 InputStream is = process.getInputStream(); 10 InputStreamReader isr = new InputStreamReader(is); 11 BufferedReader br = new BufferedReader(isr); 12 String line = null; 13 StringBuffer sb = new StringBuffer(); 14 while ((line = br.readLine()) != null) { 15 sb.append(line); 16 } 17 is.close(); 18 isr.close(); 19 br.close(); 20 21 if (null != sb && !sb.toString().equals("")) { 22 if (sb.toString().indexOf("TTL") > 0) { 23 //网络畅通 24 return true; 25 } 26 } 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 31 return false; 32 }
原文:http://www.cnblogs.com/smallrock/p/3539556.html