ConnectivityManager:andriod中处理网路连接的类:
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.)
- Send broadcast intents when network connectivity changes
- Attempt to "fail over" to another network when connectivity to a network is lost
- 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添加权限