Key APIs and techniques for adjusting Android brightness to achieve personalized APP brightness settings

Key APIs and techniques for adjusting Android brightness to achieve personalized APP brightness settings

Android system brightness adjustment

Android system brightness adjustment is the most global, and is commonly found in the brightness setting item in the system settings. Android provides an interface for getting and setting the system brightness value ("brightness value in manual mode"), calling the corresponding API to get the current screen brightness value and set a specific value. The returned brightness value is an integer value between 0-255.

 // 获取系统亮度Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS); // 设置系统亮度Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,systemBrightness);

In Android 2.X and later systems, the "Auto Brightness" option is added to the system brightness adjustment. "Auto Brightness" automatically changes the system brightness based on the external light source. Currently, most mobile phones can also adjust the value of "Auto Brightness" slightly. The corresponding to Auto Brightness is "Manual Brightness". When in "Manual Brightness", setting and dragging the brightness progress bar will greatly change the Android system brightness. "Manual Brightness" and "Auto Brightness" are respectively called the "Brightness Mode" of the Android system.

 // 获取系统亮度模式Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE); // 设置系统亮度模式Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE,systemMode);

Android does not provide a brightness value interface in "automatic brightness" mode. The above system brightness value acquisition interface actually refers to the brightness value in "manual brightness" mode. Through the manual brightness value and the system brightness mode setting interface, most of the conventional Android system brightness setting coding requirements can be met to complete the system brightness adjustment.

Android current Window brightness adjustment

Common scenario: The screen brightness becomes brighter when opening the payment interface

 //brightness是一个0.0-1.0之间的一个float类型数值。 Window window = activity.getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.screenBrightness = brightness; window.setAttributes(lp);

By default, when we modify the system brightness value directly, the brightness effect can be immediately reflected in the current Window. This is because by default, the default value of screenBrightness of WindowManager.LayoutParams is WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE. This means that the Window does not have its own brightness parameters and will follow the changes in the system brightness effect. When the system brightness is adjusted, all Windows will immediately reflect the system brightness setting effect.

Android APP brightness adjustment

Android does not directly provide a brightness adjustment method for the APP level. If you need to adjust the brightness of the APP, you can do it indirectly through the system brightness adjustment or the current screen brightness adjustment method.

  1. Record the system brightness value and brightness mode. When the user is in the APP interface, directly modify the system brightness value. When the user exits the APP or the APP goes to the background (such as pressing the Home button, etc.), restore the recorded system brightness value.
  2. Set the brightness of each Window, and set the APP brightness value in the base class Activity to achieve brightness adjustment for each current screen.


<<:  Let’s talk about how to cleverly deal with iOS keyboard problems?

>>:  The powerful functions of Android terminal emulator Termux: application calls Termux to execute commands

Recommend

Scan QR code barcode

Source code introduction: Scan QR code barcode, s...

If a cultural relic gets sick, does it also need an IV drip?

Do you see the IV drip in the picture? That's...

How to explain sleeping in using physics?

It's really not my problem that I always want...

"Cure" or "cause cancer"? The health truth behind the aspartame controversy

According to Reuters, the artificial sweetener as...

Absolutely useful information! Nine abnormal ways of operating Taobao and Tmall

Text/E-commerce consultant Lao Lu First summarize...

Sharing of iOS network layer architecture design

[[166105]] Preface A few days ago, I helped the c...

Why does eating nuts make your saliva taste like water?

Nuts are nutritious and delicious. The reason for...

These unpopular iOS apps unlock new ways to use iPhone

The launch of the iPhone in 2007 not only ushered...