SoFunction
Updated on 2025-04-10

Android development realizes the function of adjusting screen brightness

This article describes the Android development to realize the function of adjusting screen brightness. Share it for your reference, as follows:

When entering the QR code display interface in many apps, the screen brightness will be automatically adjusted. So how to adjust the screen brightness of the app? Let me introduce it to you below:

Note:The core idea of ​​adjusting screen brightness is provided to Android systemContentProviderPerform

1. Declare permissions

Users need to be allowed to modify the system configuration

<uses-permission android:name=".CHANGE_CONFIGURATION"/>
<uses-permission android:name=".WRITE_SETTINGS"/>

2. Specific operations

/**
 * Determine whether automatic brightness adjustment is turned on
 */
public static boolean isAutoBrightness(Context context) {
  ContentResolver resolver = ();
  boolean automicBrightness = false;
  try {
   automicBrightness = (resolver,
     .SCREEN_BRIGHTNESS_MODE) == .SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
  } catch ( e) {
   ();
  }
  return automicBrightness;
}
/**
 * Get the brightness of the screen
 */
public static int getScreenBrightness(Context context) {
  int nowBrightnessValue = 0;
  ContentResolver resolver = ();
  try {
   nowBrightnessValue = (resolver, .SCREEN_BRIGHTNESS);
  } catch (Exception e) {
   ();
  }
  return nowBrightnessValue;
}
/**
 * Set the brightness when the current activity is displayed
 * The maximum screen brightness value is generally 255, and each phone is different
 * The value range of screenBrightness is between [0,1]
 */
public static void setBrightness(Activity activity, int brightness) {
   lp = ().getAttributes();
   = (brightness) * (1f / 255f);
  ().setAttributes(lp);
}
/**
 * Turn on and off automatic brightness adjustment
 */
public static boolean autoBrightness(Context activity, boolean flag) {
  int value = 0;
  if (flag) {
   value = .SCREEN_BRIGHTNESS_MODE_AUTOMATIC; //Open  } else {
   value = .SCREEN_BRIGHTNESS_MODE_MANUAL;//closure  }
  return ((),
    .SCREEN_BRIGHTNESS_MODE,
    value);
}
/**
 * Save the brightness setting status, and exit the app can also maintain the setting status
 */
public static void saveBrightness(Context context, int brightness) {
  ContentResolver resolver = ();
  Uri uri = (.SCREEN_BRIGHTNESS);
  (resolver, .SCREEN_BRIGHTNESS, brightness);
  (uri, null);
}

OK, the brightness adjustment is almost the same.

In addition, more instructions on Android permission control can be viewed here.Android permissions operation instructions

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Android file operation skills summary》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.