wifi广播接收器多个意图

时间:2016-11-01 13:04:22

标签: java android broadcastreceiver android-broadcastreceiver wifimanager

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

我在Manifest

中使用了它
<action android:name="android.net.wifi.STATE_CHANGE" />

用于注册广播接收器。

以及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手机进行测试。

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

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

1 个答案:

答案 0 :(得分:3)

当您使用intent-filter android.net.wifi.STATE_CHANGE时,它会多次触发广播接收器。例如,如果您打开wifi,则广播的onreceive将在1-5次的任何地方多次呼叫。因此,它会为您提供多个NetworkInfo.State.CONNECTED,同样也会断开连接。

因此,如果你想只有一个连接或断开它输出意味着只需使用一个布尔标志并正确设置它,首先如果启用wifi,意味着它应该忽略剩余的wifi连接意图,同样wifi也断开连接。 / p>