Android brightness adjustment is divided into three levels: Android system brightness adjustment, Android App brightness adjustment, and Android current screen (Window) brightness adjustment

Android brightness adjustment

The brightness Settings of the Android operating system are the highest globally and are common in the brightness Settings of the system. Android provides an interface to get and set the system brightness value (” brightness value in manual mode “)

* On Android, the brightness value ranges from 0 to 255. The value is an int

Obtaining system Brightness

Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
Copy the code

Setting the System Brightness

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,125);
Copy the code

To set the system brightness, you need to add the permission to modify the system Settings

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Copy the code

To adjust the screen brightness, set the screen brightness mode to manual

After Android 2.1, the “Auto Brightness” option has been added to the system brightness adjustment

“Automatic brightness” is to automatically change the brightness of the system according to the external light source. At present, most mobile phones can also adjust the value of “automatic brightness” to a small extent

The corresponding automatic brightness is “Manual Brightness”. Under “Manual Brightness”, setting and dragging the brightness progress bar will greatly change the Brightness of the Android system

“Manual brightness” and “Auto brightness” are called the “brightness mode” of Android, respectively.

In contrast, the Android system also provides an interface to get and set the “brightness mode”

Make it clear that there are two modes of adjusting screen brightness

  1. Settings.system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC: Set the value to 1 to automatically adjust the brightness
  2. Settings.system. SCREEN_BRIGHTNESS_MODE_MANUAL: The value is 0 and manual mode is used

Obtain the system brightness mode

Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
Copy the code

Set the system brightness mode

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
Copy the code

Android App brightness adjustment

Unlike system brightness, Android does not provide a direct way to adjust brightness at the App level. Therefore, the brightness adjustment of the App can be achieved indirectly through the system brightness adjustment or the current screen brightness adjustment

Android current screen (Window) brightness adjustment

Android provides an interface for setting the brightness of the current screen (Window)

Window window = this.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness;
window.setAttributes(lp);
Copy the code

Note that here the brightness is a value of type float between 0.0 and 1.0

By default, if you change the brightness value directly, the brightness effect will be displayed in the current Window. This is because by default, WindowManager. LayoutParams screenBrightness default values for the WindowManager. LayoutParams. BRIGHTNESS_OVERRIDE_NONE

This means that Window does not have its own brightness parameters and will change according to the system brightness effect. This is the most common: when you adjust the brightness of the system, all Windows immediately reflect the system brightness Settings

Refer to the article

www.cnblogs.com/lwbqqyumidi…