wifi广播接收器多个意图

wifi broadcast receiver multiple intents

我开发了一个使用 Wifi 广播接收器的应用程序。我的要求是当 Wifi 连接到路由器时通过获取 IP 地址获取函数调用,以及当 Wifi 与先前连接的路由器断开连接时调用另一个函数。

我在清单中使用了这个

<action android:name="android.net.wifi.STATE_CHANGE" />

用于注册广播接收器。

和广播接收器 class 在 OnReceive 中:

NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
NetworkInfo.State state = networkInfo.getState(); 

if (state == NetworkInfo.State.CONNECTED) {

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wi = wifiManager.getConnectionInfo();

    Log.e("++++WiFi Conected","++++WiFi Conected");

    Flags.wifiState = true;
    updateWifiSyncTrue(context);

    if (wi.getIpAddress() != 0) {
        intent = new Intent(context, ConnectionService.class);
        intent.putExtra("intentValue", "signalOn");
        intent.putExtra("bssId", wi.getBSSID());
        context.startService(intent);
    }
}

if (state == NetworkInfo.State.DISCONNECTED) {
    Log.e("++++WiFi DisConected","++++WiFi DisConnected");
    Flags.wifiState = false;
    updateWifiSyncFalse(context);
    updateRouterSyncFalse(context);

    intent = new Intent(context, ConnectionService.class);
    intent.putExtra("intentValue", "signalOff");
    context.startService(intent);
}

KitKat 之前一切正常 但是当我用棒棒糖版本测试这个时,On Receive 调用了很多次。

当我打开 WiFi 时,结果 状态 : 空闲 -- 正在连接 -- 正在验证 -- 强制检查 -- 已连接 -- 已连接。 有时也会断开连接。

同时关闭。 已连接 -- 断开连接 -- 断开连接 -- 断开连接 -- 断开连接

NetworkInfo调试结果如下:

[type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/AUTHENTICATING, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/AUTHENTICATING, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/CAPTIVE_PORTAL_CHECK, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/CAPTIVE_PORTAL_CHECK, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "DJ     Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false].

我恳请在这方面帮助我。在 wifi 打开时使用我的代码 CONNECTED 状态执行多次并且 DISCONNECTED 状态也在执行。

并且WIFI关闭时生成连接状态,然后生成4-5断开状态。

我使用 HTC phone 进行测试。

我使用了简单的编码,我是否必须为此进行更深入的条件检查? (详细状态等...)

处理此解决方案的最佳方法是什么?

当您使用 intent-filter android.net.wifi.STATE_CHANGE 时,它会多次触发广播接收器。例如,如果您打开 wifi,则广播的 onreceive 将在 1-5 次之间的任何地方多次调用。所以,它会给你多个 NetworkInfo.State.CONNECTED 和断开连接也一样。

因此,如果您只想连接一个或断开连接,它的输出意味着只需使用一个布尔标志并以一种方式正确设置它,首先如果启用了 wifi 意味着它应该忽略剩余的 wifi 连接意图,同样 wifi 也断开连接.