欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > Android Settings 设置项修改

Android Settings 设置项修改

2025/5/3 5:38:19 来源:https://blog.csdn.net/snowgeneral/article/details/142965534  浏览:    关键词:Android Settings 设置项修改

Settings 设置项

在 Android 系统上,WRITE_SETTINGS 这个权限从 API 1 就已经开始有了。

通过在 app 中设置权限 android.permission.WRITE_SETTINGS 允许 app 读/写 系统设置

在官方文档的描述中,还有一段注意事项:

Note: If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user’s approval by sending an intent with action Settings.ACTION_MANAGE_WRITE_SETTINGS. The app can check whether it has this authorization by calling Settings.System.canWrite().

意思是在 app 的目标 api 是 23 (Android 6) 或更高版本时,app 用户必须显示地通过一个权限管理页面授权 app 拥有 读/写 系统设置的权限。使用 IntentSettings.ACTION_MANAGE_WRITE_SETTINGS 打开这个权限管理页面,让用户决定是否授权给 app 读/写 系统设置。在 app 中可以调用 Settings.System.canWrite() 查询系统设置是否可以修改。

ACTION_MANAGE_WRITE_SETTINGS

上面描绘中提到 ACTION_MANAGE_WRITE_SETTINGS 这个 action。 下面是它完整的定义。

package android.provider;/*** The Settings provider contains global system-level device preferences.*/
public final class Settings {// ....../*** Activity Action: Show screen for controlling which apps are allowed to write/modify* system settings.* <p>* In some cases, a matching Activity may not exist, so ensure you* safeguard against this.* <p>* Input: Optionally, the Intent's data URI can specify the application package name to* directly invoke the management GUI specific to the package name. For example* "package:com.my.app".* <p>* Output: Nothing.*/@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)public static final String ACTION_MANAGE_WRITE_SETTINGS ="android.settings.action.MANAGE_WRITE_SETTINGS";// ......
}

使用 IntentACTION_MANAGE_WRITE_SETTINGS 打开一个权限管理页面,提示用户打开开关授权用户 读取/修改 系统设置。但是也存在例外情况,有的设备上不存在这样一个 Activity 页面,所以在使用这个 action 前,先确认安全措施中是否包含这样的一个权限管理页面。

在使用时,需要创建在 Intent 中携带 Uri 数据,将具体的 app 的 packagename 传入,系统根据 Uri 内的 packagename 快速地打开页面并显示 app 的基本数据,例如 app 图标,app 名称。

要打开这个页面,下方的 “Allow modifying system settings” 开关正常显示,在项目的 manifest 文件中声明 WRITE_SETTINGS 权限。

 <uses-permission android:name="android.permission.WRITE_SETTINGS" />

在上面的操作页面,打开开关后,然后返回上一个 Activity 页面,返回的 ActivityResult 的数据。

ActivityResult{resultCode=RESULT_CANCELED, data=null}

在修改了权限之后,返回到上一个页面,不会带回任何值。

不可更改的设置项

一次在项目中遇到了涉及到 app 修改系统 “Set time automatically” 项需求,但在 app 获取到 WRITE_SETTINGS 权限后,调用 API 去修改 Settings.Global.AUTO_TIME ,会发现返回是 失败 的结果。

 val setResult = Settings.System.putInt(content

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词