com.android.shell没有获得此权限:android.permission.WRITE_SETTINGS

时间:2019-06-30 03:28:51

标签: android adb

我正在尝试通过以下方式设置屏幕亮度:

adb shell settings put system screen_brightness 150 

但是我得到一个错误:

java.lang.SecurityException: com.android.shell was not granted  this permission: android.permission.WRITE_SETTINGS.
    at android.provider.Settings.isCallingPackageAllowedToPerformAppOpsProtectedOperation(Settings.java:13454)
    at android.provider.Settings.checkAndNoteWriteSettingsOperation(Settings.java:13336)
    at com.android.providers.settings.SettingsProvider.mutateSystemSetting(SettingsProvider.java:1690)
    at com.android.providers.settings.SettingsProvider.insertSystemSetting(SettingsProvider.java:1629)
    at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:494)
    at android.content.ContentProvider$Transport.call(ContentProvider.java:483)
    at com.android.providers.settings.SettingsService$MyShellCommand.putForUser(SettingsService.java:387)
    at com.android.providers.settings.SettingsService$MyShellCommand.onCommand(SettingsService.java:285)
    at android.os.ShellCommand.exec(ShellCommand.java:103)
    at com.android.providers.settings.SettingsService.onShellCommand(SettingsService.java:51)
    at android.os.Binder.shellCommand(Binder.java:634)
    at android.os.Binder.onTransact(Binder.java:532)
    at android.os.Binder.execTransact(Binder.java:731)

1 个答案:

答案 0 :(得分:0)

要根据文档使用WRITE_SETTINGS:

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

在清单中添加以上行,对于Android M或更高版本的设备,请使用此行

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(getApplicationContext())) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, 200);

            }
        }