Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Show the video description

In this video we will learn, how to react to WiFi state changes by registering a BroadcastReceiver with the WIFI_STATE_CHANGED_ACTION IntentFilter. The wifi state broadcast is a “sticky broadcast”, which means that it stays in the system and our BroadcastReceiver’s onReceive method will be triggered as soon as we call registerBroadcastReceiver and everytime we change the WiFi state.
We register our receiver in onStart and unregister it in onStop. This way we can update our UI when the user for example enables/disables the WiFi in the quick settings dropdown menu. In this example we will react to WIFI_STATE_ENABLED and WIFI_STATE_DISABLED and update our user interface there. To access the WiFi state we have to set the ACCESS_WIFI_STATE and CHANGE_WIFI_STATE permissions in the AndroidManifest.xml file.

AndroidManifest.xmlactivity_main.xmlMainActivity.java