Android之ConnectivityManager

ConnectivityManagerandriod中处理网路连接的类:


Api解释:

Class that answers queries about the state of network                        connectivity. It also notifies applications when networkconnectivity changes. 

Get an instance of this class by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE).


Theprimary responsibilities of this class are to:

Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)

 

  1. Send broadcast intents when network connectivity changes
  1. Attempt to "fail over" to another network when connectivity to a network is lost
  1. Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks

 

说的就是这个类,是关于网络状态连接的类,当手机网络连接改变时,会通知application.

通过Context.getSystemService(Context.CONNECTTIVITY_SERVICE)获取相应实例。

 

用法:

1.判断是否有网路连接:


	public boolean isNetWorkConnected(Context context){
		if(context!=null){
			ConnectivityManager mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager.getActivieNetworkInfo();
			if(mNetworkInfo != null){
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}


   2.判断WIFI网络是否可用


public boolean isWifiConnected(Context context) {  
    if (context != null) {  
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context  
                .getSystemService(Context.CONNECTIVITY_SERVICE);  
        NetworkInfo mWiFiNetworkInfo = mConnectivityManager  
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
        if (mWiFiNetworkInfo != null) {  
            return mWiFiNetworkInfo.isAvailable();  
        }  
    }  
    return false;  
}  


 

      3)判断MOBILE网络是否可用

 


 

	public boolean isMobileConnected(Context context) {  
	    if (context != null) {  
	        ConnectivityManager mConnectivityManager = (ConnectivityManager) context  
	                .getSystemService(Context.CONNECTIVITY_SERVICE);  
	        NetworkInfo mMobileNetworkInfo = mConnectivityManager  
	                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
	        if (mMobileNetworkInfo != null) {  
	            return mMobileNetworkInfo.isAvailable();  
	        }  
	    }  
	    return false;  
	}  

     4)获取当前网络连接的类型信息

public static int getConnectedType(Context context) {  
	    if (context != null) {  
	        ConnectivityManager mConnectivityManager = (ConnectivityManager) context  
	                .getSystemService(Context.CONNECTIVITY_SERVICE);  
	        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();  
	        if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {  
	            return mNetworkInfo.getType();  
	        }  
	    }  
	    return -1;  
	}  


 

 

方法总结:1.通过上下文获取ConnectivtyManager对象

  2.通过ConnectivtyManager获取NetworkInfo对象

    如果判断是否有网络连接通过ConnectivtyManager中的getActiveNetworkInfo()方法

 有哪种连接在getNetworkInfo中传入相应的字段(具体去查API);

  3.判断网络是否可用。

 4 .AndroidManifest添加权限

你可能感兴趣的:(Android之ConnectivityManager)