小米10获取系统亮度
通过Settings.System.SCREEN_BRIGHTNESS返回的系统亮度在0-255之间,然而在米10手机中测试发现返回的是0FFFH,即0-4095之间。其他安卓手机是否有此变化还有待考证。
下面附上获取/更改系统亮度代码
public class Brightness {
/*
* 获取系统亮度
* */
public static int getSystemBrightness(ContentResolver cr){
int systemBrightness=0;
try {
systemBrightness = Settings.System.getInt(cr,Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return systemBrightness;//0~4095
}
/*
* 更改当前界面亮度
* */
public static void changeAppBrightness(Activity activity,int brightness){
Window window = activity.getWindow();
WindowManager.LayoutParams layoutParams=window.getAttributes();
if (brightness==-1){
layoutParams.screenBrightness=WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
}else {
layoutParams.screenBrightness=(brightness<=0 ? 1:brightness)/4095f;
}
window.setAttributes(layoutParams);
}
}