/** * 判断网络是否已连接 */ public static boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if (info != null) { return info.isConnected(); } return false; }
/** * 使用ping判断网络连接 */ public static boolean ping() { try { Process process = Runtime.getRuntime().exec("ping -c 1 www.baidu.com"); int exitCode = process.waitFor(); return (exitCode == 0); } catch (Exception e) { e.printStackTrace(); } return false; }