Can't apply system screen brightness programmatically in Android Can't apply system screen brightness programmatically in Android android android

Can't apply system screen brightness programmatically in Android


OK, found the answer here:Refreshing the display from a widget?

Basically, have to make a transparent activity that processes the brightness change. What's not mentioned in the post is that you have to do:

Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0);Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightnessLevel); 

then do

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

And if you call finish() right after applying the changes, brightness will never actually change because the layout has to be created before the brightness settings is applied. So I ended up creating a thread that had a 300ms delay, then called finish().


I'm doing something similar with screen brightness in one of my apps, and I'm doing it through the WindowManager and it works. I'm using the following code to get the current screen brightness (and save it for later) and set it to full:

    WindowManager.LayoutParams lp = getWindow().getAttributes();    previousScreenBrightness = lp.screenBrightness;    float brightness = 1;    lp.screenBrightness = brightness;     getWindow().setAttributes(lp); 


Use the answer given by "user496854" above
If you are taking max screenBrightness =255 then while doing

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

divide screenBrightness by 255 like

WindowManager.LayoutParams lp = getWindow().getAttributes();lp.screenBrightness = brightness/(float)255;   getWindow().setAttributes(lp);