Business needs to adjust the interface brightness, convenient two-dimensional code scan, record.

The default brightness value ranges from 0 to 255. You can adjust the brightness in either of the following ways

  • Current interface brightness
  • System brightness
Adjust the brightness of the current interface

The auto brightness adjustment mode takes effect after you exit the current screen.

/ * * * * * set the current interface brightness values @ param activity * @ param aimBrightness * / public static void setCurrentScreenBrightness (activity activity, @IntRange(from = 0, to = 255) int aimBrightness) { WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes(); LayoutParams. ScreenBrightness = Float. The valueOf (aimBrightness / 255.0 f); activity.getWindow().setAttributes(layoutParams); }Copy the code
Adjust system brightness

Apply for WRITE_SETTINGS permission to adjust system brightness (note permission applications after 6.0).

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

Note: The system brightness can be set dynamically only in manual mode, so you need to operate the brightness mode in advance, and then adjust the brightness.

The getContentResolver() method in the Activity is used to get the ContentResolver instance and realize data access to the ContentProver of the system. Settings.system. SCREEN_BRIGHTNESS_MODE Gets the current screen brightness adjustment mode, where

  • SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1Indicates the automatic adjustment mode
  • SCREEN_BRIGHTNESS_MODE_MANUAL = 0Indicates manual adjustment mode
/** * @param aimam */ public static void setSystemBrightness(Activity) @IntRange(from = 0, to = 255) int aimBrightness) { setToManualBrightness(activity); Uri uri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS); ContentResolver resolver = activity.getContentResolver(); Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, aimBrightness); activity.getContentResolver().notifyChange(uri, null); } /** * Set to manual ** @return */ public static void setToManualBrightness(@nonnull Context Context) {ContentResolver resolver = context.getContentResolver(); try { int brightMode = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE); If (brightMode == settings.system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {// Set settings.system. putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); } } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); }}Copy the code
other

Get the current screen brightness by setting.System.SCREEN_BRIGHTNESS

/ * * * * * for current interface brightness values @ param context * @ return * / public static int getCurrentScreenBrightness (@ NonNull context context) {  ContentResolver resolver = context.getContentResolver(); int currentBrightness = 0; try { currentBrightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return currentBrightness; }Copy the code

Refer to the article