Change adaptive brightness level programmatically

2019-06-05 13:28发布

问题:

How can I change the adaptive brightness level programmatically, on Android Lollipop?

I know how to change the manual brightness level, and to toggle on or off the adaptive brightness. It is done like that: Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, newLevel);

However, with adaptive brightness is enabled, the OS combines it with another brightness level which is different than the manual one.

Is there a way to do this?

Target/min/max SDK is 21.

回答1:

Don't know why but there is a hidden constant SCREEN_AUTO_BRIGHTNESS_ADJ in Android API to adjust adaptive brightness. But you can pass "screen_auto_brightness_adj" string value instead like I did.

Adaptive brightness adjustment is stored as float value in range [-1;1]. If you use brightness value in range [0;255], you can convert it to proper value as shown below.

float value = (((float)brightness*2)/255) - 1.0f;
Settings.System.putFloat(contentResolver, "screen_auto_brightness_adj", value);


回答2:

If your app targetSdkVersion is 23+, Settings.System.putFloat(contentResolver, "screen_auto_brightness_adj", value) won't work as Android disables you to modify any "hidden" settings.

Read frameworks\base\packages\SettingsProvider\src\com\android\providers\settings\SettingsProvider.java warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk() for details.