Android中的屏幕亮度值

你如何获得当前的活动屏幕亮度?

通过调用setBrightness(100)以下方法可以很好地将亮度设置为max:

 private void setBrightness(int brightness) { WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = brightness/100.0f; getWindow().setAttributes(layoutParams); } 

我试图达到以下目的:

  1. 开始活动并保存当前的亮度值
  2. 将亮度设置为最大
  3. 将某些事件的亮度重置为初始值

非常感谢!

Solutions Collecting From Web of "Android中的屏幕亮度值"

尝试

 int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS); 

接着

  WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = curBrightnessValue/100.0f; getWindow().setAttributes(layoutParams); 

将亮度重置为默认值

 WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = -1f; getWindow().setAttributes(layoutParams); 

小于0的值(默认值)意味着使用首选的屏幕亮度。

请参阅http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness

我在Android 17+上使用这个(可能适用于较低的API)

  private void setScreenBrightnessTo(float brightness) { WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes(); if (lp.screenBrightness == brightness) { return; } lp.screenBrightness = brightness; getActivity().getWindow().setAttributes(lp); } 

用途:

将屏幕设置为“最大亮度”(例如,当您显示条形码或其他内容时):

setScreenBrightnessTo(BRIGHTNESS_OVERRIDE_FULL);

重置回用户以前的任何东西

setScreenBrightnessTo(BRIGHTNESS_OVERRIDE_NONE);