简体   繁体   English

在使用自动模式时以编程方式更改亮度在 Android 中无效

[英]Changing programmatically brightness while using auto mode has no effect in Android

I've written code to create DialogPreference that should be working similar to default Android brightness setting dialog.我已经编写了代码来创建 DialogPreference,它的工作方式应该类似于默认的 Android 亮度设置对话框。

It works perfect as long as brightness mode is set to Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL .只要亮度模式设置为Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL它就可以完美运行。 But when I change mode to Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC the phone brightness does not take the value of Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS) into account.但是当我将模式更改为Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC ,手机亮度不会考虑Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS)

Let me show you an example:让我给你看一个例子:

  1. I set brightness in Android settings to 255 and mode to automatic.我在 Android 设置中将亮度设置为 255,将模式设置为自动。 The changes are reflected by the phone (as well as my app).更改会通过手机(以及我的应用程序)反映出来。
  2. I set brightness in Android settings to 100 and mode to manual.我在 Android 设置中将亮度设置为 100,将模式设置为手动。 The changes are reflected by the phone (as well as my app).更改会通过手机(以及我的应用程序)反映出来。
  3. I set brightness in my app to 50 and mode is still manual.我在我的应用程序中将亮度设置为 50,并且模式仍然是手动的。 The changes are reflected by the phone (as well as my app).更改会通过手机(以及我的应用程序)反映出来。
  4. I set brightness in my app to 50 and mode to automatic.我将应用程序中的亮度设置为 50,将模式设置为自动。 Although I programmatically change current screen brightness to 50, the rest of my app and the phone itself (for example phone settings and home) has brightness 255 and mode to automatic (which I can check in Android settings).虽然我以编程方式将当前屏幕亮度更改为 50,但我的应用程序的其余部分和手机本身(例如手机设置和主页)的亮度为 255,模式为自动(我可以在 Android 设置中查看)。 At the same time, Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS) is still returning 50.同时, Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS)仍然返回50。

Here is my code.这是我的代码。 I as said, it's in a class that extends DialogPreference, thus the onCreateDialogView method.我说过,它在一个扩展 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;
    }
}

I have <uses-permission android:name="android.permission.WRITE_SETTINGS" /> in my AndroidManifest.xml我的 AndroidManifest.xml 中有<uses-permission android:name="android.permission.WRITE_SETTINGS" />

Does anyone know what is going on?有谁知道发生了什么? I have seen some Stack Overflow threads with similar problems, but none of them got any helpful answer.我见过一些 Stack Overflow 线程有类似问题,但没有一个得到任何有用的答案。

If you set brightness mode to automatic.如果将亮度模式设置为自动。 then you need to stop automatic brightness adjustment before sets brightness.那么你需要在设置亮度之前停止自动亮度调整。 this code worked for me:这段代码对我有用:

      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);
        }

It seems that it just works that way.似乎它就是这样工作的。 If you set brightness mode to automatic by yourself, you cannot manipulate brightness level.如果您自行将亮度模式设置为自动,则无法操作亮度级别。

An example is here: BrightnessPreference code on grepcode.com for 4.1.2 In this Preference brightness SeekBar is disabled when brightness mode is automatic.示例如下: grepcode.com 上的 BrightnessPreference 代码 4.1.2在此首选项中,当亮度模式为自动时,亮度 SeekBar 被禁用。

Based on BrightnessPreference code on grepcode.com for 4.4.2 I came up with the following inflexible solution:基于grepcode.com 上 4.4.2 的 BrightnessPreference 代码,我想出了以下不灵活的解决方案:

@Override
protected void onClick() {
    Intent intent = new Intent("com.android.settings.BrightnessPreferenceActivity");
    startActivity(intent);
}

It just starts the Activity used in Android settings.它只是启动 Android 设置中使用的 Activity。 It was good enough in my case.就我而言,这已经足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM