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?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With