android判断是否有网络
1
public static boolean checkNet(Activity act) { ConnectivityManager manager = (ConnectivityManager) act .getApplicationContext().getSystemService( Context.CONNECTIVITY_SERVICE); if (manager == null) { return false; } NetworkInfo networkinfo = manager.getActiveNetworkInfo(); if (networkinfo == null || !networkinfo.isAvailable()) { return false; } return true; }
?2 大同小异
public static boolean checkNet_(Context context) {ConnectivityManager mConnectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo info = mConnectivity.getActiveNetworkInfo();if (info == null) {return false;}if (!info.isAvailable() || !mConnectivity.getBackgroundDataSetting()) {return false;} else {return true;}}
?3
private int checkNet__(Context context) {ConnectivityManager connectionManager = (ConnectivityManager) context.getSystemService("connectivity");NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();if(networkInfo != null){if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {return 1; //返回1是 WIFI网络} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {return 2; //返回 2是移动互联网()} else {return 3; //返回3是 未知网络}}else{return 0;}}?