gpt4 book ai didi

android: ConnectivityManager.CONNECTIVITY_ACTION,网络连接状态未改变时发送广播

转载 作者:行者123 更新时间:2023-11-30 03:56:29 25 4
gpt4 key购买 nike

我注册了一个广播来获取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 , 我听不懂。

编辑:我的意思是,我在 Activity 的初始化中注册了一个过滤器为“ConnectivityManager.CONNECTIVITY_ACTION”的广播,当网络始终连接时,我启动 Activity ,执行函数“onReceive”;在 google doc说“A change in network connectivity has occurred”,但是网络没有变化,为什么要执行onReceive?

最佳答案

由于 CONNECTIVITY_ACTION 是固定广播,因此它会继续调用 onRecevie 将其删除,您可以使用 removeStickyBroadcast(Intent intent)DOC

关于android: ConnectivityManager.CONNECTIVITY_ACTION,网络连接状态未改变时发送广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com