zoukankan      html  css  js  c++  java
  • Android工具类-关于网络、状态的工具类

    下方是一个很好的监测网络、状态的工具类

    public class NetworkUtils {
    
    	/**
    	 * 网络是否可用
    	 * 
    	 * @param activity
    	 * @return
    	 */
    	public static boolean isNetworkAvailable(Context context) {
    		ConnectivityManager connectivity = (ConnectivityManager) context
    				.getSystemService(Context.CONNECTIVITY_SERVICE);
    		if (connectivity == null) {
    		} else {
    			NetworkInfo[] info = connectivity.getAllNetworkInfo();
    			if (info != null) {
    				for (int i = 0; i < info.length; i++) {
    					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
    						return true;
    					}
    				}
    			}
    		}
    		return false;
    	}
    
    
    	/**
    	 * Gps是否打开
    	 * 
    	 * @param context
    	 * @return
    	 */
    	public static boolean isGpsEnabled(Context context) {
    		LocationManager locationManager = ((LocationManager) context
    				.getSystemService(Context.LOCATION_SERVICE));
    		List<String> accessibleProviders = locationManager.getProviders(true);
    		return accessibleProviders != null && accessibleProviders.size() > 0;
    	}
    
    	/**
    	 * wifi是否打开
    	 */
    	public static boolean isWifiEnabled(Context context) {
    		ConnectivityManager mgrConn = (ConnectivityManager) context
    				.getSystemService(Context.CONNECTIVITY_SERVICE);
    		TelephonyManager mgrTel = (TelephonyManager) context
    				.getSystemService(Context.TELEPHONY_SERVICE);
    		return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
    				.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
    				.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
    	}
    
    	/**
    	 * 判断当前网络是否是wifi网络
    	 * if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) { 
    	 * 
    	 * @param context
    	 * @return boolean
    	 */
    	public static boolean isWifi(Context context) {
    		ConnectivityManager connectivityManager = (ConnectivityManager) context
    				.getSystemService(Context.CONNECTIVITY_SERVICE);
    		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    		if (activeNetInfo != null
    				&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
    			return true;
    		}
    		return false;
    	}
    
    	/**
    	 * 判断当前网络是否3G网络
    	 * 
    	 * @param context
    	 * @return boolean
    	 */
    	public static boolean is3G(Context context) {
    		ConnectivityManager connectivityManager = (ConnectivityManager) context
    				.getSystemService(Context.CONNECTIVITY_SERVICE);
    		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    		if (activeNetInfo != null
    				&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
    			return true;
    		}
    		return false;
    	}
    }

    以上方法均通过测试,tv_result为自设定的TextView。

    				tv_result.append("网络是否可用:"+NetworkUtils.isNetworkAvailable(MainActivity.this)+"
    ");
    				tv_result.append("GPS开关是否打开:"+NetworkUtils.isGpsEnabled(MainActivity.this)+"
    ");
    				tv_result.append("是否为3G网络:"+NetworkUtils.is3G(MainActivity.this)+"
    ");
    				tv_result.append("WIFI是否打开:"+NetworkUtils.isWifiEnabled(MainActivity.this)+"
    ");
    				tv_result.append("是否为WIFI网络:"+NetworkUtils.isWifi(MainActivity.this)+"
    ");






  • 相关阅读:
    DataReader相关知识点⭐⭐⭐⭐⭐
    C# Distanct List集合
    RePlace函数
    DataTable和DataRow和DataColumn ⭐⭐⭐⭐⭐
    scrapy 基础组件专题(八):scrapy-redis 框架分析
    scrapy 基础组件专题(九):scrapy-redis 源码分析
    scrapy 基础组件专题(七):scrapy 调度器、调度器中间件、自定义调度器
    scrapy 基础组件专题(六):自定义命令
    scrapy 基础组件专题(五):自定义扩展
    scrapy 基础组件专题(四):信号运用
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3400197.html
Copyright © 2011-2022 走看看