如何设置屏幕亮度?

MD.*_*.MD 1 android screen brightness

我想通过点击按钮来调整屏幕亮度,所以当背景为白色时,屏幕亮度应该是最大的,同时如果背景为黑色,屏幕亮度应该是最小的,但是我得到一个错误:NullPointerException ...这里是我的代码:

public void lamp2(boolean mode){

        if(mode){

            r.setBackgroundColor(Color.WHITE);
            btn.setText("Turn OFF");
            btn.setTextColor(Color.RED);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = 90 / 100.0f;
            getWindow().setAttributes(lp);
            this.mode = true;
        }

        else if(!mode){

            r.setBackgroundColor(Color.BLACK);
            btn.setText("Turn ON");
            btn.setTextColor(Color.GREEN);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = 100 / 100.0f;
            getWindow().setAttributes(lp);
            this.mode = false;
        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 7

把这些线放到最大

WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);
Run Code Online (Sandbox Code Playgroud)

把这些线放到最低限度

WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0.1f;
getWindow().setAttributes(params);
Run Code Online (Sandbox Code Playgroud)