Monday, 20 July 2015

WiFi state change Listener (Receiver) Android

There is no listener like OnClick listener to listen to WiFi status/state change during an activity.

To Listen to WiFi state change we have a to register a receiver that receives WiFi state change.

The following code helps you to listen to WiFi status change while running app in foreground

1.Add these permissions  to your Manifest file.


These are normal (PROTECTION_NORMAL) permissions. So you don't need check at runtime.

2. Create a receiver that has to run on receiving WiFi state change


3. create a boolean that check the registration of receiver.

This is required to avoid registering or deregistering the receiver multiple times.

4. Register your receiver in onResume method of the activity.

5. UnRegister the receiver when ever you pause the activity to avoid leak of receiver.

1 comment :