gpt4 book ai didi

java - 在 Android 29 上启用禁用 WiFi

转载 作者:行者123 更新时间:2023-12-03 16:50:40 25 4
gpt4 key购买 nike

public boolean WifiManager.setWifiEnabled (boolean enabled)

This method was deprecated in API level 29. Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always return false and will have no effect. If apps are targeting an older SDK ( Build.VERSION_CODES.P or below), they can continue to use this API.



我们如何在 Android 29 上禁用 WiFi?

最佳答案

Android Q(Android 10,API 级别 29)您不能再以编程方式启用/禁用 wifi。使用设置面板切换 wifi 连接:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
startActivityForResult(panelIntent, 0)
} else {
// add appropriate permissions to AndroidManifest file (see https://stackoverflow.com/questions/3930990/android-how-to-enable-disable-wifi-or-internet-connection-programmatically/61289575)
(this.context?.getSystemService(Context.WIFI_SERVICE) as? WifiManager)?.apply { isWifiEnabled = true /*or false*/ }
}

关于java - 在 Android 29 上启用禁用 WiFi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58006340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com