我注册了一个广播来获取ConnectivityManager.CONNECTIVITY_ACTION,代码是:
registerReceiver(mNetwrokBroadcastReceiver, new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
接收者是:
private BroadcastReceiver mNetwrokBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isConnected = false;
NetworkInfo mNetworkInfo = intent
.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo.State state = (mNetworkInfo == null ? NetworkInfo.State.UNKNOWN
: mNetworkInfo.getState());
switch (state) {
case CONNECTED:
isConnected = true;
break;
case DISCONNECTING:
if (intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false))
isConnected = false;
else {
isConnected = true;
}
break;
default:
break;
}
if (isConnected) {
mPopwin.onDismiss();
init();
}
}
};
但我发现当网络始终连接时,它也会发送广播,为什么??? 有一个question,我无法得到它。
编辑: 我的意思是,我在一个活动的init中注册一个过滤器是“ConnectivityManager.CONNECTIVITY_ACTION”的广播,当网络一直连接时,我启动活动,执行“onReceive”功能;在google doc中说“发生了网络连接的变化”,但网络没有变化,为什么要执行onReceive?