2

我已經開發了一個使用Wifi廣播接收機的應用程序。我的要求是當Wifi通過獲取IP地址連接到路由器時獲得函數調用,並且當Wifi從先前連接的路由器斷開連接時調用另一個函數。wifi廣播接收機多種意圖

我用這個清單中

<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); 
} 

每一件事情一直工作到奇巧 但是,當我測試這個棒棒糖版本在接收呼叫很多次。

當我打開我的WiFi其導致爲 國家: 空閒--Connecting--身份驗證 - 圈養檢查 - 連接 - 連接。 有時也斷開連接。

關閉時。 連接 - 斷開 - 斷開 - 斷開 - 斷開

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手機進行測試。

我使用簡單的編碼,我必須得到更深入的條件檢查嗎? (詳細狀態等)

處理此解決方案的最佳方法是什麼?

+0

感謝編輯和修正 –

回答

3

當您使用intent-filter android.net.wifi.STATE_CHANGE時,它會多次觸發廣播接收器。例如,如果您正在切換wifi,則廣播的onreceive將在1-5次的任意位置調用多次。所以,它會給你多個NetworkInfo.State.CONNECTED和相同的斷開也。

所以如果你想只有一個連接或斷開輸出意味着只需使用一個布爾標誌,並設置正確的方式,首先如果啓用wifi啓用意味着它應該忽略剩餘的WiFi連接意圖和類似wifi斷開連接也。

+0

**感謝**的答案Sivateja.koppineedi –