申请Settings.ACTION_MANAGE_WRITE_SETTINGS权限


在android 5.0及以上平台,若需对手机设置进行修改(如 屏幕亮度),则需申请Settings.ACTION_MANAGE_WRITE_SETTINGS权限,而通过一般方式无法申请

手机设置进行修改(如 屏幕亮度)


  // 根据当前进度改变亮度
                Settings.System.putInt(getActivity().getContentResolver(),
                        Settings.System.SCREEN_BRIGHTNESS, tmpInt);
                tmpInt = Settings.System.getInt(getActivity().getContentResolver(),
                        Settings.System.SCREEN_BRIGHTNESS, -1);
                WindowManager.LayoutParams wl = getActivity().getWindow().getAttributes();

                float tmpFloat = (float) tmpInt / 255;
                if (tmpFloat > 0 && tmpFloat <= 1) {
                    wl.screenBrightness = tmpFloat;
                }
                getActivity().getWindow().setAttributes(wl);

设置自动调节:

 
  
 // 开启亮度自动调节

    public static void startAutoBrightness(Context context) {

        Settings.System.putInt(context.getContentResolver(),

                Settings.System.SCREEN_BRIGHTNESS_MODE,

                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);


//              Settings.System.putInt(context.getContentResolver(),
//                Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);

        Uri uri = Settings.System
                .getUriFor("screen_brightness");
        context.getContentResolver().notifyChange(uri, null);
    }
   
 // 停止自动亮度调节

    public static void stopAutoBrightness(Context context) {

        Settings.System.putInt(context.getContentResolver(),

                Settings.System.SCREEN_BRIGHTNESS_MODE,

                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);



    }

以上都需要申请Settings.ACTION_MANAGE_WRITE_SETTINGS权限
 
  
 
  

使用Google示例

easypermissions


仍然无法申请

在主界面做下以申请即可

 private void CheckPermission() {
        if (!Settings.System.canWrite(MainUIActivity.this)) {


            Uri selfPackageUri = Uri.parse("package:"
                    + getPackageName());
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
                    selfPackageUri);
            startActivity(intent);
        }
    }







你可能感兴趣的:(Android,Studio,测试工具开发,android手机项目开发)