调整Android手机屏幕亮度

2014-11-24 08:29:37 · 作者: · 浏览: 1

贴一段调整安卓手机屏幕亮度的代码 ,可以参考使用,见下:


public void setBrightness(int level) {
ContentResolver cr = getContentResolver();
Settings.System.putInt(cr, "screen_brightness", level);
try {
defaultLevel = Settings.System.getInt(cr, "screen_brightness");
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
Window window = getWindow();
LayoutParams attributes = window.getAttributes();
float flevel = level;
attributes.screenBrightness = flevel / 255;
getWindow().setAttributes(attributes);
}


此外,UiModeManager这个类提供了控制系统UI模式的服务,可以参考使用:


{
UiModeManager uim = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
int i = uim.getCurrentModeType();


if (i != Configuration.UI_MODE_TYPE_CAR) {
uim.enableCarMode(0);
}


uim.setNightMode(UiModeManager.MODE_NIGHT_YES);


}