当 Wifi 连接时,CONNECTIVITY_ACTION 意图收到两次 [英] CONNECTIVITY_ACTION intent received twice when Wifi connected
问题描述
在我的应用程序中,我有一个 BroadcastReceiver
,它通过
标签作为组件启动,过滤 android.net.conn.CONNECTIVITY_CHANGE
意图.
In my app I have a BroadcastReceiver
that is launched as a component through a <receiver>
tag, filtering android.net.conn.CONNECTIVITY_CHANGE
intents.
我的目标只是知道何时建立了 Wifi 连接,所以我在 onReceive()
中所做的是:
My goal is simply to know when a Wifi connection was established, so what I am doing in onReceive()
is this:
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected()) {
// Wifi is connected
}
它工作正常,但是当建立 Wifi 连接时,我似乎总是在大约一秒钟内得到两个相同的意图.我试图查看可以从 Intent、ConnectivityManager
和 WifiManager
中获得的任何信息,但我找不到任何可以区分这两种 Intent 的信息.
It works fine, but I always seem to get two identical intents within about one second when a Wifi connection is established. I tried to look at any info I could get from the intent, the ConnectivityManager
and WifiManager
, but I can't find anything that distinguishes the two intents.
查看日志,至少还有一个 BroadcastReceiver
也接收到两个相同的意图.
Looking at the log, there is at least one other BroadcastReceiver
that also receives the two identical intents.
它在运行 Android 2.2 的 HTC Desire 上运行
It is running on a HTC Desire with Android 2.2
知道为什么我在 Wifi 连接时似乎得到重复"意图或两者之间的区别是什么吗?
Any idea why I seem to get a "duplicated" intent when Wifi connects or what the difference between the two might be?
推荐答案
注意:有关最近的最新答案,请参阅 下面这个!
NOTE: For a recent, up-to-date answer, see this one below!
经过大量的谷歌搜索和调试,我相信这是确定 Wifi 是否已连接或断开的正确方法.
After a lot of googling and debugging, I believe this is the correct way to determine if Wifi has connected or disconnected.
BroadcastReceiver 中的 onReceive()
方法:
The onReceive()
method in the BroadcastReceiver:
public void onReceive(final Context context, final Intent intent) {
if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo networkInfo =
intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if(networkInfo.isConnected()) {
// Wifi is connected
Log.d("Inetify", "Wifi is connected: " + String.valueOf(networkInfo));
}
} else if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
NetworkInfo networkInfo =
intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
! networkInfo.isConnected()) {
// Wifi is disconnected
Log.d("Inetify", "Wifi is disconnected: " + String.valueOf(networkInfo));
}
}
}
连同 AndroidManifest.xml 中的以下接收器元素
Together with the following receiver element in AndroidManifest.xml
<receiver android:name="ConnectivityActionReceiver"
android:enabled="true" android:label="ConnectivityActionReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
一些解释:
当只考虑
ConnectivityManager.CONNECTIVITY_ACTION
时,当 Wifi 连接时,我总是得到两个包含相同 NetworkInfo 实例的意图(getType() == TYPE_WIFI 和 isConnected() == true)——此问题中描述的问题.
When only considering
ConnectivityManager.CONNECTIVITY_ACTION
, I always get two intents containing identical NetworkInfo instances (both getType() == TYPE_WIFI and isConnected() == true) when Wifi connects - the issue described in this question.
仅使用WifiManager.NETWORK_STATE_CHANGED_ACTION
时,Wifi断开时不会广播intent,而是包含不同NetworkInfo实例的两个intent,允许在Wifi连接时确定一个事件.
When only using WifiManager.NETWORK_STATE_CHANGED_ACTION
, there is no intent broadcasted when Wifi disconnects, but two intents containing different NetworkInfo instances, allowing to determine one event when Wifi is connected.
注意:我收到了一份崩溃报告 (NPE),其中 intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO)
返回 null.因此,即使这种情况看起来极其罕见,添加空检查也可能是个好主意.
NOTE: I've received one single crash report (NPE) where the intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO)
returned null. So, even if it seems to be extremely rare to happen, it might be a good idea to add a null check.
干杯,托斯滕
这篇关于当 Wifi 连接时,CONNECTIVITY_ACTION 意图收到两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!