gpt4 book ai didi

android - WIFI_SLEEP_POLICY_NEVER 如何在 API-17 中设置?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:02:12 25 4
gpt4 key购买 nike

我正在使用以下代码,我的目标是 API-15

android.provider.Settings.System.putInt(cr, 
android.provider.Settings.System.WIFI_SLEEP_POLICY,
android.provider.Settings.System.WIFI_SLEEP_POLICY_NEVER);

当代码在 API-17 平台上运行时,我在 logcat 中收到警告,

设置 wifi_sleep_policy 已从 android.provider.Settings.System 移动到 android.provider.Settings.Global,值未更改。

所以我之后所做的就是将我的项目目标设置为 APi-17 并使用此代码

        if(Build.VERSION.SDK_INT < 17)
{
android.provider.Settings.System.putInt(cr,
android.provider.Settings.System.WIFI_SLEEP_POLICY,
android.provider.Settings.System.WIFI_SLEEP_POLICY_NEVER);
}
else
{
android.provider.Settings.Global.putInt(cr,
android.provider.Settings.Global.WIFI_SLEEP_POLICY,
android.provider.Settings.Global.WIFI_SLEEP_POLICY_NEVER);

}

因此,我开始在平台 Api-17 中关注 SecurityException

         java.lang.SecurityException: Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS

然后我检查了 WRITE_SECURE_SETTINGS 权限是否仅适用于系统应用程序,我无法用它编译我的代码,就好像此权限仅适用于系统应用程序一样。

所以我很困惑是我之前收到的警告是错误的还是我的代码有什么问题,我想让它与 API-17 兼容。

最佳答案

不幸的是,从 API-17 开始无法再更改此设置,因为它 has been deprecated .

如您所说,WRITE_SECURE_SETTINGS 权限仅授予系统应用程序,因此最好的替代方法是要求用户从 wifi 设置中手动设置此选项:

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

关于android - WIFI_SLEEP_POLICY_NEVER 如何在 API-17 中设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14062068/

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