我已經開發了一個使用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手機進行測試。
我使用簡單的編碼,我必須得到更深入的條件檢查嗎? (詳細狀態等)
處理此解決方案的最佳方法是什麼?
感謝編輯和修正 –