public static boolean checkNet(Context context) { try { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo info = connectivity.getActiveNetworkInfo(); if (info != null && info.isConnected()) { if (info.getState() == NetworkInfo.State.CONNECTED) { return true; } } } } catch (Exception e) { return false; } return false; }
?
?
//打印判断结果 System.out.println(checkNet(this));
?
?
另外,一定要加入权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
?