Question

I want to get the previous screen brightness when the Brightness in the Display settings is in the auto-mode.

I try to use the following code:

WindowManager.LayoutParams lp = getWindow().getAttributes();
previousScreenBrightness = lp.screenBrightness;

but the value of the previousScreenBrightness is -1.0 all the time even though I put the phone in the dark or light place.

I can get the right value, when the Brightness setting isn't in the auto-mode.

I'm using Android 4.2.

Was it helpful?

Solution

You should use

int previousScreenBrightness = android.provider.Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);

to get system brightness value (same with the brightness value in Settings>Display>Brightness).(Range from 0 to 255)

What you get (-1.0) from WindowManager.LayoutParams indicates BRIGHTNESS_OVERRIDE_NONE, which means your current Activity brightness value is not override by WindowManager.LayoutParams attributes. When the value set to 0 to 1, then your current Activity brightness value would influenced by the setting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top