This article describes the method of setting screen brightness by Android programming. Share it for your reference, as follows:
Use scenarios
Recent researchAndroidL SettingsThe code of the code is written to control the brightness of the screen.
In fact, there are many applications for adjusting screen brightness. For example, many video applications respond to touch events to adjust brightness.
Screen brightness adjustment mode
First of all, it is necessary to clarify that there are two adjustment modes for screen brightness:
.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
: The value is 1, and the brightness is automatically adjusted..SCREEN_BRIGHTNESS_MODE_MANUAL
: The value is 0, manual mode.
If you need to achieve brightness adjustment, you need to set the screen brightness adjustment mode to manual mode.
The setup method is as follows:
public void setScrennManualMode() { ContentResolver contentResolver = getActivity().getContentResolver(); try { int mode = (contentResolver, .SCREEN_BRIGHTNESS_MODE); if (mode == .SCREEN_BRIGHTNESS_MODE_AUTOMATIC) { (contentResolver, .SCREEN_BRIGHTNESS_MODE, .SCREEN_BRIGHTNESS_MODE_MANUAL); } } catch ( e) { (); } }
Get screen brightness value
Here you need to know:
1. The maximum screen brightness is 255.
2. The minimum screen brightness is 0.
3. The screen brightness value range must be: 0~255.
How to set the screen brightness:
private int getScreenBrightness() { ContentResolver contentResolver = getActivity().getContentResolver(); int defVal = 125; return (contentResolver, .SCREEN_BRIGHTNESS, defVal); }
Set the system screen brightness value
Before setting the brightness of the system screen, you need to ensure thatThe following permissions are declared:
When the screen brightness mode is 0, that is, manually adjust, the screen brightness can be set by the following code:
private void saveScreenBrightness() { setScrennManualMode(); ContentResolver contentResolver = getActivity().getContentResolver(); int value = 255; // Set the brightness value to 255 (mContentResolver, .SCREEN_BRIGHTNESS, value); }
Set the current window brightness
When many video applications handle screen brightness when touch events, they do not modify the system brightness value, but modify the brightness of the window where the current application is located. The specific method is to modify the screenBrightness property in LayoutParams. The reference code is as follows:
private void setWindowBrightness(int brightness) { Window window = getWindow(); lp = (); = brightness / 255.0f; (lp); }
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.