WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION never starts

I register the receiver on onResume() :

 registerReceiver(wifiConnectivityReceiver, new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)); 

This is the receiver itself:

 class WiFiConnectivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED,false)){ Log.d(TAG,"Connected to network!"); } else { Log.d(TAG,"Could not connect to network!"); } } } 

In my application, I can connect to the selected WiFi network, but this SUPPLICANT_CONNECTION_CHANGE_ACTION never starts. If I change it to SUPPLICANT_STATE_CHANGED_ACTION , for example, it works.

I am working on ICS.
Has anyone else encountered such problems with this intention?

+6
source share
2 answers

It seems to me that the following code will help you:

 public void installMyReceiver(){ if (I) Log.i(TAG, "installMyReceiver() - Online"); mFlag = true; myReceiver = new BroadcastReceiver(){ public void onReceive (Context context, Intent intent){ String action = intent.getAction(); if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)){ SupplicantState supplicantState = (SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE); if (supplicantState == (SupplicantState.COMPLETED)){ if (I) Log.i(TAG, "SUPPLICANTSTATE ---> Connected"); //do something } if (supplicantState == (SupplicantState.DISCONNECTED)){ if (I) Log.i(TAG, "SUPPLICANTSTATE ---> Disconnected"); //do something } } } }; IntentFilter mFilter = new IntentFilter (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); this.registerReceiver (myReceiver, mFilter); } 

This is an easy way to get the information you want, and then perform some action. Hope this helps you!

+4
source

Has anyone else encountered similar issues with this?

Yes I. It seems that on some devices, intent is never advertised by the operating system. (Yes, I did a factory-reset device.) I ended up adding a watchdog counter to check WifiManager.isWifiEnabled() to find out about the changes. Of course, this will always be a little delayed.

Remember to remove the callback to the same Runnable instance from the Handler in the BroadcastReceiver again if you do so, so as not to process the event twice if your code is running on a device that declares the intent.

0
source

All Articles