Android Development and Realization of Screen Brightness Adjustment Function

  • 2021-08-28 21:12:19
  • OfStack

In this paper, an example is given to describe the development of Android to adjust the brightness of the screen. Share it for your reference, as follows:

In many app, when entering the 2D code display interface, the screen brightness will be automatically adjusted, so how to adjust the screen brightness of app? Let me introduce you:

Note: The core idea of adjusting screen brightness is provided for Android ContentProvider Perform an operation

1. Declare permissions

Need to allow users to modify system configuration


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

2. Specific operation


/**
*  Determine whether the automatic brightness level is turned on 
*/
public static boolean isAutoBrightness(Context context) {
  ContentResolver resolver = context.getContentResolver();
  boolean automicBrightness = false;
  try {
   automicBrightness = Settings.System.getInt(resolver,
     Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
  } catch (Settings.SettingNotFoundException e) {
   e.printStackTrace();
  }
  return automicBrightness;
}
/**
*  Gets the brightness of the screen 
*/
public static int getScreenBrightness(Context context) {
  int nowBrightnessValue = 0;
  ContentResolver resolver = context.getContentResolver();
  try {
   nowBrightnessValue = android.provider.Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return nowBrightnessValue;
}
/**
*  Sets the current Activity Brightness when displayed 
*  Maximum screen brightness value 1 Be general 255 Different types of mobile phones are different 
* screenBrightness  The value range of is in the [0,1] Between 
*/
public static void setBrightness(Activity activity, int brightness) {
  WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
  lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
  activity.getWindow().setAttributes(lp);
}
/**
*  Turn on and off automatic brightness level 
*/
public static boolean autoBrightness(Context activity, boolean flag) {
  int value = 0;
  if (flag) {
   value = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; // Open 
  } else {
   value = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;// Shut down 
  }
  return Settings.System.putInt(activity.getContentResolver(),
    Settings.System.SCREEN_BRIGHTNESS_MODE,
    value);
}
/**
*  Save the brightness setting status and exit app It can also keep the setting state 
*/
public static void saveBrightness(Context context, int brightness) {
  ContentResolver resolver = context.getContentResolver();
  Uri uri = android.provider.Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
  android.provider.Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
  resolver.notifyChange(uri, null);
}

Ok, brightness level is almost like this.

In addition, for more instructions on Android permission control, please click here to view Android permission operation instructions

More readers interested in Android can check the topics of this site: "Introduction and Advanced Tutorial of Android Development", "Summary of View Skills in Android View", "Summary of activity Operation Skills in Android Programming", "Summary of Android File Operation Skills", "Summary of Android Resource Operation Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: