我正在尝试制作一个可以改变屏幕亮度的滑块。我已经设法用上面的代码做到了
亮度代码:
BackLightControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
float BackLightValue = (float)progress/100.0f;
int brightnessMode;
try {
brightnessMode = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE);
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
BackLightSetting.setText(String.valueOf(BackLightValue));
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
}
问题是它正在工作,我可以在滑动滑块时看到亮度发生变化,但是一旦活动完成,它就会恢复到以前的亮度级别,这是没有意义的。使用滑块时禁用的自动背光设置仍然存在。我做错了什么?另外,如何防止滑块为 0,因为屏幕关闭,并且只有强制关闭应用程序才能重置更改。