标签:
一、判断网络连接是否可用
1 public static boolean isNetworkAvailable(Context context) { 2 ConnectivityManager cm = (ConnectivityManager) context 3 .getSystemService(Context.CONNECTIVITY_SERVICE); 4 if (cm == null) { 5 } else { 6 //如果仅仅是用来判断网络连接 7 //则可以使用 cm.getActiveNetworkInfo().isAvailable(); 8 NetworkInfo[] info = cm.getAllNetworkInfo(); 9 if (info != null) { 10 for (int i = 0; i < info.length; i++) { 11 if (info[i].getState() == NetworkInfo.State.CONNECTED) { 12 return true; 13 } 14 } 15 } 16 } 17 return false; 18 }
二、判断GPS是否打开
1 public static boolean isGpsEnabled(Context context) { 2 LocationManager lm = ((LocationManager) context 3 .getSystemService(Context.LOCATION_SERVICE)); 4 List<String> accessibleProviders = lm.getProviders(true); 5 return accessibleProviders != null && accessibleProviders.size() > 0; 6 }
三、判断WIFI是否打开
1 public static boolean isWifiEnabled(Context context) { 2 ConnectivityManager mgrConn = (ConnectivityManager) context 3 .getSystemService(Context.CONNECTIVITY_SERVICE); 4 TelephonyManager mgrTel = (TelephonyManager) context 5 .getSystemService(Context.TELEPHONY_SERVICE); 6 return ((mgrConn.getActiveNetworkInfo() != null && mgrConn 7 .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel 8 .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS); 9 }
四、判断是否是3G网络
1 public static boolean is3rd(Context context) { 2 ConnectivityManager cm = (ConnectivityManager) context 3 .getSystemService(Context.CONNECTIVITY_SERVICE); 4 NetworkInfo networkINfo = cm.getActiveNetworkInfo(); 5 if (networkINfo != null 6 && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) { 7 return true; 8 } 9 return false; 10 }
五、判断是wifi还是3g网络,用户的体现性在这里了,wifi就可以建议下载或者在线播放。
1 public static boolean isWifi(Context context) { 2 ConnectivityManager cm = (ConnectivityManager) context 3 .getSystemService(Context.CONNECTIVITY_SERVICE); 4 NetworkInfo networkINfo = cm.getActiveNetworkInfo(); 5 if (networkINfo != null 6 && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) { 7 return true; 8 } 9 return false; 10 }
标签:
原文地址:http://www.cnblogs.com/huolongluo/p/5697609.html