如何改变亮度?

How to change brightness?

I 运行 Runnable 应该逐渐改变亮度

private Runnable runnable = new Runnable(){

        @Override
        public void run(){
            if(intensity < 1){ intensity += intensityGrow; }
            if(intensity > 1){ intensity = 1f; }
            Log.e("intensity", intensity + "/grow "+intensityGrow);

            android.provider.Settings.System.putInt(
                getContentResolver(),

                android.provider.Settings.System.SCREEN_BRIGHTNESS,(int)(255*intensity)
            );
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

            am.setStreamVolume(
                AudioManager.STREAM_ALARM, (int) (am.getStreamMaxVolume(AudioManager.STREAM_ALARM) * intensity), 0
            );
            if(intensity < 1){
                Log.e("intensity", "continue");

                handler.postDelayed(runnable, 1000);
            }

        }
    };

我也试过用

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = intensity;
        getWindow().setAttributes(lp);

清单中有<uses-permission android:name="android.permission.WRITE_SETTINGS"/> 也没有结果。

试试这个,

new Thread() {
    public void run() {
        for (int i = initial; i < target; i++) {
            final int bright = i;
            handle.post(new Runnable() {
                public void run() {
                    float currentBright = bright / 100f;
                    window.getAttributes().screenBrightness = currentBright;
                    window.setAttributes(window.getAttributes());
                });
            }
            try {
                sleep(step);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}.start();

首先要使此工作正常,需要将亮度模式设置为手动

Settings.System.putInt(
                        getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
                    );

然后用两种方法改变亮度

android.provider.Settings.System.putInt(
                getContentResolver(),

                android.provider.Settings.System.SCREEN_BRIGHTNESS, (int) (255 * intensity)
            );
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = intensity;
            getWindow().setAttributes(lp);