Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't apply system screen brightness programmatically in Android

I'm using the following to set the system auto brightness mode and level:

    android.provider.Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0);
    android.provider.Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, y.brightness1);

I can change auto-brighess on and off, and set different levels. The settings seem to be applied properly -- I can go to into Settings --> Display --> Brightness, and whanever setting I set is actually shown correctly. However, the actual screen isn't changing its brightness. If i just tap on the slider in Display Settings, then everything gets applied.

I shoudl mention that I'm running an app withat a main activity, and these settings are getting applied in the BroadcastReceiver. I did try to create a dummy activity and tested the stuff there, but got the same results.

like image 382
user496854 Avatar asked Feb 17 '11 17:02

user496854


People also ask

How do I override my screen brightness?

Most Android phones have a similar auto brightness feature. Here's how Samsung says you can turn it off. 📲 On Android: Settings > Display > Tap the slider next to Adaptive brightness and switch it to the off position. Then, adjust the brightness bar until you've reached your desired level of brightness.

How do I not let Apps control brightness?

Go to Settings → Display → Smart Stay and disable this feature. Check if the brightness level still changes on its own.

Is there a app to make my Android screen brighter?

TwilightTwilight is an excellent app for controlling the brightness of your phone's screen. The app automatically adjusts the light to match the time of day and in a manner that doesn't impair your vision.


1 Answers

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().

like image 154
user496854 Avatar answered Sep 22 '22 09:09

user496854