android.permission.WRITE_SETTINGS 是否只授予系统应用程序?

Is android.permission.WRITE_SETTINGS only granted to system apps?

我们目前正在开发一个应用程序,我们想在其中更改一些系统设置,当然要获得用户许可。 android 文档说要执行此操作,您必须添加以下权限:

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

此外,必须明确要求用户启用此权限。 https://developer.android.com/reference/android/Manifest.permission#WRITE_SETTINGS 的摘录说:

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().

到这里,已经很清楚了。但是,当将权限添加到 AndroidManifest.xml 文件时,Android Studio 会抱怨“Permission is only granted to system apps”。现在,这令人困惑,因为我没有找到任何文档说明它确实只授予系统应用程序。

所以,我想问一下是否有人遇到过这个问题,是否有某种文档可以详细解释这个问题?或者我只是错过了什么?

如用户 passsy in the Whosebug provided by Brian 所述,android.permission.WRITE_SETTINGSandroid:protectionLevel"signature",这使得该权限自 API 起无法在用户应用程序中使用23级。

有关权限保护级别的说明,请参阅 https://developer.android.com/guide/topics/manifest/permission-element#plevel

您需要专门从用户那里获得用户许可,我的意思是您必须将用户带到一个屏幕,用户可以在该屏幕上向您的应用授予 WRITE_SETTINGS 权限。

所以当你想更改系统设置时,你必须像这样检查用户是否已授予权限:

private boolean checkSystemWritePermission() {
    boolean retVal = true;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        retVal = Settings.System.canWrite(this);
        Log.d("TAG", "Can Write Settings: " + retVal);
        if(retVal){
            ///Permission granted by the user
        }else{
            //permission not granted navigate to permission screen
            openAndroidPermissionsMenu();
        }
    }
    return retVal;
}

private void openAndroidPermissionsMenu() {
    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
    intent.setData(Uri.parse("package:" + this.getPackageName()));
    startActivity(intent);
 }