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.判断是否有网路连接:


<span style="font-size:18px;"><span style="font-family:Consolas;">	</span><span style="font-family:Courier New;">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;
	}</span></span><span style="font-family:Consolas;font-size: 14pt;">
</span>


   2.判断WIFI网络是否可用


<span style="font-family:Courier New;font-size:18px;">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;  
}  </span><span style="font-family:Consolas;font-size: 14pt;">
</span>


 

      3)判断MOBILE网络是否可用

 


 

<span style="font-family: Consolas; font-size: 14pt;">	</span><span style="font-family:Courier New;font-size:18px;">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;  
	}  </span><span style="font-family: Consolas; font-size: 14pt;">
</span>

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

<span style="font-family:Courier New;font-size:18px;">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;  
	}</span><span style="font-family:Consolas;font-size: 14pt;">  
</span>


 

 

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

  2.通过ConnectivtyManager获取NetworkInfo对象

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

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

  3.判断网络是否可用。

 4 .AndroidManifest添加权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值