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
Xem thêm Bài Viết:
- Chia sẻ đầy đủ về cách hướng dẫn Ghost Win Xp bằng USB cơ bản từ A tới Z
- Hướng dẫn tạo file ghost win 10 thông qua 3 bước đơn giản mà bạn không nên bỏ qua
- Mách bạn 4 bước trong cách bung file tib theo chuẩn UEFI đơn giản nhất
- Mật bí 4 kiểu hyperlink trong excel bị lỗi phổ biến mà bạn hay gặp nhất
- Khái niệm ma alt là gì và cách sử dụng bảng mã ATL như thế nào?
is it possible to enable the mobile data this way ? how ?
Very elaborate. Thank You
Very good, clean explanation as usually. Is it possible to check also if the dataconnection is disable or not?Thanks!
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!
Thanks Man!!! Very helpfull !!!
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.
Thanks bro
how can we get this to work with API 23?
Awesome video, thanks a lot for you
Your tutorials are amazing ! When I hear your voice, I know i'll understand !
thanks a lot for your videos you make things to look easy by explaining everything in details
Off-speaker: Werner Herzog.
hey i want to create an internet filter for android device. can any one help me?
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?
Excellent. Simple thanks
how we check the strength of a network?
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
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
Create video , get screenshot crop and save gallary please.. help..
Cool Tutorial!!!!!
How about mobile network switch
Sir Enable your Discussion tab in your channel. So, we can direct communicate with you. Please make a video to download images from firebase.