Detect WiFi State Changes with BroadcastReceiver – Android Studio Tutorial

22
49



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.

Example code:

____________________
❗ Subscribe to the channel:

⏯ Watch more tutorials:

⭐ Become a channel member for exclusive tutorials and other perks:

㊙ Help translating the videos:

💚 Support the content:

💬 Join the Discord chat:

🎧 Audiobooks and podcasts for programmers:

👶 How to get started with Android development:

📣 Follow Coding in Flow on social media:

Facebook:

Instagram:

Twitter:

Blog:

Nguồn: https://quydinh.com

Xem thêm bài viết khác: https://quydinh.com/cong-nghe

22 COMMENTS

  1. Very good, clean explanation as usually. Is it possible to check also if the dataconnection is disable or not?Thanks!

  2. Good day, thanks for your tutorials. In this lesson, this lines not needed (commented)
    private BroadcastReceiver wifiStateReceiver = new BroadcastReceiver() {

    ..
    switch (wifiStateExtra) {

    case WifiManager.WIFI_STATE_ENABLED:

    wifiSwitch.setChecked(true);

    // wifiSwitch.setText("WiFi is ON");

    break;

    case WifiManager.WIFI_STATE_DISABLED:

    wifiSwitch.setChecked(false);

    // wifiSwitch.setText("WiFi is OFF");

    break;

    ..
    your already created listener in onCreate.
    Thank you again, your tutorials is best!

  3. If you are following this video and if you are using the AVD with an API of 29 or 25 this will not work. Make sure to create a new AVD with an API level of 26.

  4. case WifiManager.WIFI_STATE_ENABLED:
    wifiSwitch.setChecked(true);
    wifiSwitch.setText("wi-fi is ON");
    wifiSwitch.setTextColor(Color.parseColor("#9dff2d"));
    wifiManager.setWifiEnabled(false);
    wifiSwitch.setChecked(false);
    wifiSwitch.setText("wi-fi is OFF");
    wifiSwitch.setTextColor(Color.parseColor("#c6010b"));
    break;

    i wrote like this for when wifi becomes to enable turn off wi-fi. It works but i want to app works also when application works in background. How can i do that?

  5. It makes a video of how to use this same method in MainActivity for Ethernet cable(TYPE_ETHERNET).
    It's possible?
    This one is working perfectly thank you very much. one more like

  6. I am a Beginner in programming in Android and now I am sitting in front of this problem: I would code a Broadcast Receiver, which gets fired whenever the Phone gets charged or "discharged". So, registered the Receiver in the Manifest.xml:
    <receiver
    android:name=".Starter"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
    <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
    </intent-filter>
    </receiver>
    In the Class of the Broadcast Receiver, I wrote a Toast into the onReceive()-Method, which should be performed, when the Receiver would work:
    public class Starter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    Toast.makeText(context,"blablabla",Toast.LENGTH_LONG).show();
    }
    }
    I am so thankful for your Help, LG M

  7. Sir Enable your Discussion tab in your channel. So, we can direct communicate with you. Please make a video to download images from firebase.

LEAVE A REPLY

Please enter your comment!
Please enter your name here