在android平台中ConnectivityManager主要负责查询网络连接状态以及在连接状态有变化的时候发出通知。
其主要的功能职责如下:
一、功能:
1、 监视网络状态,包括(Wi-Fi、GPRS、UMTS等)
2、 当网络状态发生变化时发送广播通知
3、 当网络连接失败后会尝试连接其他网络
4、 为App提供API,用于获取网络状态信息等
二、接口:
常用的有getActivieNetworkInfo、getAllNetworkInfo、getNetworkInfo(int networkType)等,这些方法返回NetworkInfo,该类用于描述网络的状态(mobile和wifi)。
在NetworkInfo中定义了两个内部类,用于枚举网络的状态,两个内部类分别为:
- NetworkInfo.DetailedState,精确的网络状态;
- NetworkInfo.State,粗略的网络状态。
在NetworkInfo中除了两个内部类外,还提供了一些方法,如
- getDetailedState():返回当前精确的网络状态。
- getState():返回当前粗略的网络状态。
- getType():返回当前的网络类型(mobile或者wi-fi)
- getTypeName():返回当前网络类型的名称。如“WIFI”或者“MOBILE”
- isAvailable():判断网络是否有效可用。
- isConnected():判断网络是否已连接。
三、案例
public void getNetworkState(View v){
//获取
ConnectivityManager connectivityManager=
(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
StringBuilder sb = new StringBuilder();
sb.append("TypeName:").append(networkInfo.getTypeName()).append("\n");
sb.append("Type:").append(networkInfo.getType()).append("\n");
sb.append("isAvailable:").append(networkInfo.isAvailable()).append("\n");
sb.append("isConnected:").append(networkInfo.isConnected()).append("\n");
NetworkInfo.DetailedState state= networkInfo.getDetailedState();
String detailedState="";
if(state== NetworkInfo.DetailedState.AUTHENTICATING){
detailedState="AUTHENTICATING";
}else if(state== NetworkInfo.DetailedState.BLOCKED){
detailedState="BLOCKED";
}else if(state== NetworkInfo.DetailedState.CONNECTED){
detailedState="CONNECTED";
}else if(state== NetworkInfo.DetailedState.CONNECTING){
detailedState="CONNECTING";
}else if(state== NetworkInfo.DetailedState.DISCONNECTED){
detailedState="DISCONNECTED";
}else if(state== NetworkInfo.DetailedState.DISCONNECTING){
detailedState="DISCONNECTING";
}else if(state== NetworkInfo.DetailedState.FAILED){
detailedState="FAILED";
}else if(state== NetworkInfo.DetailedState.IDLE){
detailedState="IDLE";
}else if(state== NetworkInfo.DetailedState.SCANNING){
detailedState="SCANNING";
}else if(state== NetworkInfo.DetailedState.OBTAINING_IPADDR){
detailedState="OBTAINING_IPADDR";
}else if(state== NetworkInfo.DetailedState.SUSPENDED){
detailedState="SUSPENDED";
}
sb.append("DetailedState:").append(detailedState).append("\n");
showState.setText(sb.toString());
}
/**
* 获取搜有网络
* @param v
*/
public void getNetwork(View v){
ConnectivityManager connectivityManager=
(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
StringBuilder stringBuilder = new StringBuilder();
NetworkInfo[] networkInfos= connectivityManager.getAllNetworkInfo();
for(NetworkInfo info:networkInfos){
stringBuilder.append("Name:").append(info.getTypeName())
.append(";isAvailable:").append(info.isAvailable()).append("\n");
}
showNetwork.setText(stringBuilder.toString());
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")){
ConnectivityManager connectivityManager=
(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info= connectivityManager.getActiveNetworkInfo();
if(info!=null){
if(info.getState()== NetworkInfo.State.CONNECTED){
showChange.setText("网络变化:网络已连接!");
}else{
showChange.setText("网络变化:网络已断开!");
}
}else{
showChange.setText("网络变化:无网络!");
}
}
}
};
注意,以上代码需要使用的如下权限:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
四、题外话:
wifiStateTracker源码中,存在startMonitoring函数注册的广播里向connectivityService的handler发送了message,我们已知在ConnectivityService的构造函数里面曾经开启了wifiStateTracker的startMonitoring函数以接受广播,现在就来看看connectivityService类。
内部类MyHandler。handleMessage方法中的case方法,1.EVENT_STATE_CHANGED 它接收了所有的stateTracker中发送的消息。
case NetworkStateTracker.EVENT_STATE_CHANGED:
if (info.getDetailedState() == NetworkInfo.DetailedState.FAILED) {
handleConnectionFailure(info);
} else if (state == NetworkInfo.State.DISCONNECTED) {
handleDisconnect(info);
} else if (state == NetworkInfo.State.SUSPENDED) {
handleDisconnect(info);
} else if (state == NetworkInfo.State.CONNECTED) {
handleConnect(info);
}
break;
完~~
文 | 力卉编程
网友评论