关于Android 设置屏幕亮度(适配Api23及更高版本)

       这个需求应用场景主要是在有些页面进入需要调节当前屏幕亮度,查找相关资料都是api 23之前的,看了些文档,找到了解决方案,特此记录下。

    我们知道在api 23之后权限需要动态去申请,但是有部分特殊权限这样是申请不到的,只有通过弹出设置窗口才能获取得到的。

Special permissions

There are a couple of permissions that don't behave like normal and dangerous permissions. SYSTEM_ALERT_WINDOWand WRITE_SETTINGS are particularly sensitive, so most apps should not use them. If an app needs one of these permissions, it must declare the permission in the manifest, and send an intent requesting the user's authorization. The system responds to the intent by showing a detailed management screen to the user.

For details on how to request these permissions, see the SYSTEM_ALERT_WINDOW and WRITE_SETTINGS reference entries.

All permissions provided by the Android system can be found at Manifest.permission.

以上说的是SYSTEM_ALERT_WINDOW和 WRITE_SETTINGS 是特别敏感的权限,一般app不能去使用他们,如果必须使用,首先需要在manifest中定义,并且在用的时候发送一个intent,系统会弹出一个管理页面来响应这个intent让用户来确认是否给予这个权限。

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 actionSettings.ACTION_MANAGE_WRITE_SETTINGS. The app can check whether it has this authorization by calling Settings.System.canWrite().

以上说的是我们可以通过Settings.System.canWrite()来检测是否有修改WRITE_SETTINGS权限,如果没有我们需要发一个action为Settings.ACTION_MANAGE_WRITE_SETTINGSintent给系统,系统会弹窗来让用户授权;

具体代码如下:

   public static boolean checkWriteSettingPermission(Activity context, int requestCode) {
        boolean permission;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            permission = Settings.System.canWrite(context);
        } else {
            permission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
        }
        if (!permission) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                intent.setData(Uri.parse("package:" + context.getPackageName()));
                context.startActivityForResult(intent, requestCode);
            } else {
                ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_SETTINGS}, requestCode);
            }
        }
        return permission;
    }

以上代码检测是否有WRITE_SETTINGS,没有的话去请求用户授权;


    /**
     * 获取屏幕亮度模式和屏幕亮度,并设置为手动模式调节亮度到最高
     */
    private void customScreenBrightness() {
        try {
            /**
             * * 获得当前屏幕亮度的模式
             * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度
             * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为手动调节屏幕亮度
             *
             */
            screenMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
            Log.i(TAG, "screenMode = " + screenMode);
            // 获得当前屏幕亮度值 0--255
            screenBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
            Log.i(TAG, "screenBrightness = " + screenBrightness);

            if (screenMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                setScreenMode(Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            }

            setScreenBrightness(255);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置屏幕亮度模式
     * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度
     * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为手动调节屏幕亮度
     *
     * @param value
     */
    private void setScreenMode(int value) {
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, value);
    }

    /**
     * 设置屏幕亮度
     *
     * @param value
     */
    private void setScreenBrightness(float value) {
        Window mWindow = getWindow();
        WindowManager.LayoutParams params = mWindow.getAttributes();
        float f = value / 255.0f;
        params.screenBrightness = f;
        mWindow.setAttributes(params);
        // 保存设置的屏幕亮度值
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, (int) value);
    }

以上代码是对屏幕亮度的操作;

 

 

 

具体参考有:

[1]. https://developer.android.com/guide/topics/permissions/overview

[2].https://developer.android.com/reference/android/Manifest.permission#WRITE_SETTINGS

[3]. https://stackoverflow.com/questions/32083410/cant-get-write-settings-permission

[4]. https://blog.csdn.net/android_ls/article/details/8678313

 

 

你可能感兴趣的:(Android)