CONNECTIVITY_ACTION意图一日两次当无线连接 [英] CONNECTIVITY_ACTION intent received twice when Wifi connected

查看:263
本文介绍了CONNECTIVITY_ACTION意图一日两次当无线连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序我有一个是通过标签发起的一个部件一个BroadcastReceiver,过滤android.net.conn.CONNECTIVITY_CHANGE意图。

In my app I have a BroadcastReceiver that is launched as a component through a tag, filtering android.net.conn.CONNECTIVITY_CHANGE intents.

我的目标很简单,就是知道建立一个无线连接时,所以我在做什么的的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连接。我想看看任何信息,我可以得到的意图,在ConnectivityManager和WifiManager,但我无法找到任何区别的两个目的。

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.

据上的HTC Desire采用Android 2.2运行

It is running on a HTC Desire with Android 2.2

任何想法,为什么我似乎得到复制的意图时,无线连接或者是两者之间的差异可能?

Any idea why I seem to get a "duplicated" intent when Wifi connects or what the difference between the two might be?

推荐答案

很多使用Google和调试后,我相信这是正确的做法,以确定是否有无线连接或断开。

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 ,我总是得到两个意图含有相同的NetworkInfo实例(包括的getType()== TYPE_WIFI和isConnected()==真)当无线连接 - 在这个问题上描述的问题

  • 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 ,但是没有任何意图广播时无线断开,但含有不同的NetworkInfo情况下,允许以确定一个事件时,无线两个目的连接。

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.

干杯, 托

这篇关于CONNECTIVITY_ACTION意图一日两次当无线连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆