我编写的代码用于创建DialogPreference,它应该与默认的Android亮度设置对话框类似。
只要亮度模式设置为Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
,它就可以完美运行。但是当我将模式更改为Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
时,手机亮度不会考虑Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS)
的值。
让我举个例子:
Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS)
仍然返回50。这是我的代码。我说,它是在一个扩展DialogPreference的类中,因此是onCreateDialogView方法。
private static int MAX_BRIGHTNESS_VALUE = 255;
@Override
protected View onCreateDialogView() {
View dialogView = super.onCreateDialogView();
mAutomatic = (CheckBox) dialogView.findViewById(R.id.automatic_brightness);
mBrightnessBar = (SeekBar) dialogView.findViewById(R.id.brightness_bar);
mBrightnessBar.setMax(MAX_BRIGHTNESS_VALUE);
mAutomatic.setChecked(getBrightnessMode() == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
mBrightnessBar.setProgress(getBrightness());
mAutomatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
setAutomaticBrightness();
} else {
setManualBrightness();
}
}
});
mBrightnessBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress > 10) {
setBrightness(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return dialogView;
}
private void setBrightness(int value) {
Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value);
// here I set current screen brightness programmatically
Window window = ((Activity) getContext()).getWindow();
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = value / (float) MAX_BRIGHTNESS_VALUE;
window.setAttributes(layoutpars);
}
private int getBrightness() {
try {
return Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
return MAX_BRIGHTNESS_VALUE;
}
}
private void setAutomaticBrightness() {
Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
private void setManualBrightness() {
Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
private int getBrightnessMode() {
try {
return Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
} catch (SettingNotFoundException e) {
e.printStackTrace();
return Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
}
}
我的AndroidManifest.xml中有<uses-permission android:name="android.permission.WRITE_SETTINGS" />
有谁知道发生了什么事?我已经看到一些类似问题的Stack Overflow线程,但没有一个得到任何有用的答案。
答案 0 :(得分:0)
它似乎就是这样。如果您自己将亮度模式设置为自动,则无法操纵亮度级别。
这里有一个例子: BrightnessPreference code on grepcode.com for 4.1.2 在此首选项亮度中,当亮度模式自动时,SeekBar将被禁用。
基于BrightnessPreference code on grepcode.com for 4.4.2 我提出了以下不灵活的解决方案:
@Override
protected void onClick() {
Intent intent = new Intent("com.android.settings.BrightnessPreferenceActivity");
startActivity(intent);
}
它只是启动Android设置中使用的Activity。在我的情况下这已经足够了。
答案 1 :(得分:0)
如果将亮度模式设置为自动。那么您需要先停止自动亮度调整,然后再设置亮度。 这段代码对我有用:
public static void stopAutoBrightness(Activity activity) {
android.provider.Settings.System.putInt(activity.getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE,
android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}