Reputation: 12179
I am registering 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 am able to connect to selected WiFi network,but this SUPPLICANT_CONNECTION_CHANGE_ACTION
is never fired.If I change it to SUPPLICANT_STATE_CHANGED_ACTION
for example it is working.
I am working on ICS.
Did someone else experience problems like this with this intent?
Upvotes: 6
Views: 3311
Reputation: 43
I thing 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);
}
It's a simple way to obtain the information which you want and then execute some action. I hope it can help you!
Upvotes: 4
Reputation: 10112
Did someone else experience problems like this with this intent?
Yes, me. It seems that on some devices the intent is never declared by the operating system. (Yes, I did factory-reset the device.) I ended up to additionally add a watchdog counter to check WifiManager.isWifiEnabled()
to become aware of changes. Of course, this will always be a bit delayed.
Don’t forget to remove the callback to the same instance of the Runnable
from the Handler
in your BroadcastReceiver
again if you do this, for not handling the event twice if your code executes on a device that does declare the intent.
Upvotes: 0