我编写了创建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线程,但是它们都没有任何有用的答案。
我已经编写了创建DialogPreference的代码,其工作方式应类似于默认的Android亮度设置对话框。只要将亮度模式设置为Settings.System ....
似乎它就是这样工作的。如果您自己将亮度模式设置为自动,则无法操纵亮度级别。
如果将亮度模式设置为自动。那么您需要先停止自动亮度调整,然后再设置亮度。该代码对我有用: