Recently in making an app settings item, brightness adjustment. When you do, you find that the Android brightness adjustment is more complicated than expected. In fact, there is a lot of information on the Internet, but some of the posts are misleading. Put this piece of content in your own understanding here.
Overall, Android brightness adjustment is divided into three levels, namely: Android system brightness adjustment, Android app brightness adjustment and Android current screen (Window) brightness adjustment.
1.Android System Brightness Adjustment
The Android system has the highest level of brightness and is common to the brightness settings in system settings. Android provides an interface for getting and setting the system brightness value ("Brightness value in manual mode"), as follows:
1 // Get System Brightness 2 Settings.System.getInt (Getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS);
1 // Setting the system brightness 2 Settings.System.putInt (Getcontentresolver (), settings.system.screen_brightness,systembrightness);
It is important to note that the luminance value returned is an integer value between 0-255.
After Android 2.1, the "Auto brightness" option has been added to the system brightness adjustment. "Automatic Brightness" is based on the external light source to change the brightness of the system, most of the current mobile phones in the "automatic brightness" can also be small amplitude adjustment of its value. corresponding to the automatic brightness is "manual brightness", when in the "Manual brightness", set the drag brightness progress bar will greatly change the Android system brightness. "Manual Brightness" and "auto brightness" are respectively referred to as the "brightness mode" of the Android system.
Corresponding to this, the Android system also provides an interface to get and set the "Brightness mode".
1 // Get system Brightness mode 2 Settings.System.getInt (Getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS_MODE);
1 // Setting the system brightness mode 2 Settings.System.putInt (Getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS_MODE, Systemmode);
Unfortunately, the brightness value interface in auto brightness mode is not available in Android. The above-mentioned gain system luminance value interface actually refers to the brightness value in "manual brightness" mode.
In general, by using the manual brightness value and setting the system Brightness mode interface, it is possible to meet most of the normal encoding requirements for Android system brightness settings to complete the system brightness adjustment.
2.Android app Brightness adjustment
Unlike the system brightness, Android does not directly provide a way to adjust the brightness of the app level. As a result, the brightness adjustment that needs to be done by the app can be achieved indirectly through the system brightness adjustment or the current screen brightness adjustment method.
3.Android Current screen (Window) Brightness adjustment
Android provides an interface for setting the brightness for the current screen (Window), which is commonly described as follows:
1 Window window = activity.getwindow (); 2 Windowmanager.layoutparams LP = window.getattributes (); 3 lp.screenbrightness = brightness; 4 window.setattributes (LP);
It is important to note that the brightness here is a float type value between the 0.0-1.0.
By default, when we directly modify the system brightness value, the current window can react to the brightness effect immediately, because by default, The default value for the Windowmanager.layoutparams screenbrightness is WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE.
That is, the window does not have its own brightness parameter, which changes the brightness effect of the obliged system. This is our most common: when the system brightness is adjusted, all windows immediately reflect the system brightness setting effect.
At that time, we also encountered such requirements in the actual project: the system sets the brightness only for the current window or app in effect, without affecting the brightness settings of the system itself.
Assuming a seekbar,ui in the current window is basically similar to the System Luma UI, the user can swipe this seekbar to make the current window brightness change instantly without affecting the system brightness effect. How to achieve it?
At this point we need to enable the Windowmanager.layoutparams screenbrightness parameter to have an auto-specific luminance value that, after setting this value, will override the system brightness setting in the current window range.
Therefore, it is necessary to convert the brightness value selected by the user to the corresponding window luminance value (in line with the system luminance value, assuming the maximum value of Seekbar is 255).
1 //modifies the current window brightness based on the luminance value2 Public voidChangeappbrightness (Context context,intbrightness) {3window window =( Activity) context). GetWindow ();4Windowmanager.layoutparams LP =window.getattributes ();5 if(Brightness = =-1) {6Lp.screenbrightness =WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;7}Else {8Lp.screenbrightness = (Brightness <= 0? 1:brightness)/255f;9 }Ten window.setattributes (LP); One}
Where the brightness parameter is the luminance value selected by the user.
So why is there a "brightness = =-1" Judgment in the above code? This is primarily due to the "follow system brightness" or "restore system default Brightness" settings that may appear in the app settings, and when the user does this, the screenbrightness parameter is restored directly to the default parameter value. Because the system brightness value is not directly obtained by the previous description of the "Auto brightness" mode, when the system is in "auto brightness" mode, the brightness parameter value will not be determined accurately, so Reverting the screenbrightness parameter to the default parameter value becomes an effective method.
4.Android app Brightness adjustment feasible solution analysis
At present, there are many blog posts mentioned in the app brightness adjustment, the proposed scheme is in the app settings, first record the system brightness and brightness mode before setting, the user in the app settings for brightness adjustment, directly modify the system brightness value, when the user exits the app, Or app to the background (such as pressing the home button, etc.), then restore the system brightness. Seems like a viable solution at first. But there are several problems:
1. How do I get to the system brightness and brightness mode before setting (because the system brightness value is restored to this initial value after this app)? When the user goes to the settings page each time they get it? Strictly speaking, it is impossible to record accurately. Because the user operation of Android unpredictable, such as go to the Settings page, drag Seekbar set a brightness value, this time directly modify the system brightness value, if the user does not apply to the background or the application is not quit the situation directly outside the app to modify the system brightness settings, The system brightness can be set directly by the drop-down title bar in Xiaomi. Therefore, it is difficult to obtain the system brightness initial value before the app brightness setting.
2. How can I tell if the user is outside the app? Because you need to restore the system brightness setting to the initial system brightness. If the user can press the home button, long press the home button to switch the app directly, directly back button to exit the app, drop-down title bar directly click on other app notification information into other apps, mobile phone automatically lock screen after unlocking users directly into other applications, etc. This kind of operation scenario is also unpredictable, so it is also difficult to judge the timing of the user coming outside the app to restore the system brightness to its initial value.
In this way, the app brightness adjustment scheme is recommended by setting the current screen (Window).
The general idea is as follows: When the user adjusts the brightness of the setting, the Changeappbrightness () method is called, which changes the brightness of the current screen (Window) without any effect on the brightness of the system. The next problem is finally focused on when the user comes to other activity in the app, if the brightness you just set is worth the instant response.
When the user adjusts the brightness, save the current brightness settings (such as saving to sharedpreferences), in the base class activity in the Onresume method, you can remove the user in the sharedpreferences set of app brightness value, The Changeappbrightness () method is then used to achieve the brightness adjustment for each current screen.
In general, setting the app brightness is easier and more effective by setting the current screen (Window).
Android Brightness Adjustment