简体   繁体   English

在Android设备中切换Wifi的状态

[英]Toggling state of Wifi in Android device

I have to make an app in which I need to display the status of the WiFi on a RadioGroup (Either its "on" or "off"). 我必须创建一个应用程序,我需要在RadioGroup上显示WiFi的状态(“on”或“off”)。 I want the default behavior for the application to show the status of the Wifi when the user clicks the SettingsPage. 我希望应用程序的默认行为在用户单击SettingsPage时显示Wifi的状态。 The RadioGroup must check the status of the Wifi and display it accordingly("on" or "off"). RadioGroup必须检查Wifi的状态并相应地显示它(“开”或“关”)。 I must also be able to toggle the states of the Wifi. 我还必须能够切换Wifi的状态。

I hope the images below can make my question clear. 我希望下面的图片可以清楚地表达我的问题。

Thanks for your time. 谢谢你的时间。

在此输入图像描述

Check this question: Android: How to Enable/Disable Wifi or Internet Connection Programmatically 检查此问题: Android:如何以编程方式启用/禁用Wifi或Internet连接

And make sure the radio button is selected if the wifi is on otherwise make radio button as not selected. 如果wifi打开,请确保选中单选按钮,否则将单选按钮设为未选中。

More info about checking wifi status: 有关检查wifi状态的更多信息:

WifiManager wifi = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
boolean b=wifi.isWifiEnabled();

To Enable WiFi: 启用WiFi:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);

Note: To access with WiFi state, we have to add following permissions inside the AndroidManifest.xml file: 注意:要使用WiFi状态进行访问,我们必须在AndroidManifest.xml文件中添加以下权限:

android.permission.ACCESS_WIFI_STATE
android.permission.UPDATE_DEVICE_STATS 
android.permission.CHANGE_WIFI_STATE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM