1

I run Runnable which should gradually change brightness

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);
            }

        }
    };

I also tried to use

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

There is <uses-permission android:name="android.permission.WRITE_SETTINGS"/> in Manifest Also without result.

4

2 回答 2

0

尝试这个,

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();
于 2015-09-21T13:02:58.403 回答
0

为了首先完成这项工作,必须将亮度模式设置为手动

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);
于 2015-09-21T14:20:24.800 回答