WiFi 状态未启用
我正在尝试创建一个用于启用和禁用 wifi 的小部件。
if(myWifiManager.isWifiEnabled()){
System.out.println("Toggle Wifi Enabled going to disable");
myWifiManager.setWifiEnabled(false);
}
else{
System.out.println("Wifi Disabled going to enable ");
myWifiManager.setWifiEnabled(true);
System.out.println("WI: "+myWifiManager.isWifiEnabled());
}
这是我正在使用的代码,禁用部分工作正常,但启用部分工作不正常。启用 wifi 后不久,我正在打印 wifi 状态,但我发现它是错误的。
I am trying to create a widget for enabling and disabling the wifi.
if(myWifiManager.isWifiEnabled()){
System.out.println("Toggle Wifi Enabled going to disable");
myWifiManager.setWifiEnabled(false);
}
else{
System.out.println("Wifi Disabled going to enable ");
myWifiManager.setWifiEnabled(true);
System.out.println("WI: "+myWifiManager.isWifiEnabled());
}
This is the code i am using the disabling part is working fine but the enabling part is not working fine. Soon after enabling the wifi i am printing the wifi state i am getting it as false.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是如何在 Android 中打开和关闭 wifi。
首先,您需要在清单文件中声明以下内容
在您的 Activity 类上执行此操作后
}
说明
从我们的系统获取 Wifi 服务
< strong>检查我们的 wifi 当前是否打开或关闭
打开/关闭我们的 wifi
wifiManager.setWifiEnabled();
参考
WifiEnabler
http://google-androidlovers.blogspot.com/2012/01/扫描无线网络-in-android.html
http://www.java2s。 com/Open-Source/Android/android-platform-apps/Settings/com/android/settings/wifi/WifiApEnabler.java.htm
Here is how to turn on and turn off wifi in android.
First you need to declare the following in your manifest file
After doing it that on your Activity class
}
Explanation
Get the Wifi service from our system
Check the our wifi is currently turned on or turned off
Turn on/off our wifi
wifiManager.setWifiEnabled();
Reference
WifiEnabler
http://google-androidlovers.blogspot.com/2012/01/scan-for-wireless-networks-in-android.html
http://www.java2s.com/Open-Source/Android/android-platform-apps/Settings/com/android/settings/wifi/WifiApEnabler.java.htm
(来源)
所以这仅适用于设备与 Android Pie 及以下版本。
(Source)
So this will only work for devices with android Pie and below.
下载这个例子,它就是你想要的
https://github.com/siddhpuraamitr/WIfi-Toggle-Widget
Download this example it is what you want
https://github.com/siddhpuraamitr/WIfi-Toggle-Widget