使用WifiManager开启wifi停止在Android 10上正常工作 [英] Turning on wifi using WifiManager stops to work on Android 10
问题描述
我有以下代码,这些代码以前在Android 10之前运行良好.但是,它无法在Android 10设备上打开wifi.
I have following code which used to work well pre Android 10. But it is not able to turn wifi on in Android 10 devices.
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
boolean res = wifiMgr.setWifiEnabled(true);
//res value is set to false above because setWifiEnabled returns false on Android 10
以下是我在AndroidManifest.xml
<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>
<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>
<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>
<uses-permission android:name=\"android.permission.INTERNET\"/>
<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>
我什至在动态地请求这些权限.但这似乎也无济于事.
I am even dynamically requesting for these permissions. But that doesn't seem to help as well.
问题:
Android 10上有什么变化吗?我应该做些其他事情来通过我的应用程序以编程方式打开wifi吗?
Question:
Has anything changed on Android 10? Should I do something more to turn on wifi programatically from my app?
推荐答案
公共布尔setWifiEnabled(已启用布尔)
此方法在API级别29中已弃用. 从Build.VERSION_CODES#Q开始,不允许应用程序启用/禁用Wi-Fi.
This method was deprecated in API level 29. Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi.
兼容性说明:对于以Build.VERSION_CODES.Q或更高版本为目标的应用程序,此API始终返回false,并且无效.
Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always return false and will have no effect.
如果应用程序以较早的SDK(Build.VERSION_CODES.P或更低版本)为目标,则它们可以继续使用此API.
If apps are targeting an older SDK ( Build.VERSION_CODES.P or below), they can continue to use this API.
根据文档,Apps
将无法再从Android-10 API level 29
转到Wi-Fi OFF/ON
.[直到Google提供了替代解决方案].
According to documentation, Apps
will not be able to turn Wi-Fi OFF/ON
anymore from Android-10 API level 29
[Until google provides an alternative solution].
有关更多信息,请参见官方文档.
For more information see official documentation.
并且与此有关,在Google issuetracker
中已经创建了问题128554616 .
And there is an issue 128554616 has been created in google issuetracker
about this.
这篇关于使用WifiManager开启wifi停止在Android 10上正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!