gpt4 book ai didi

android - 改变 Activity 后亮度恢复正常

转载 作者:行者123 更新时间:2023-11-29 23:51:22 25 4
gpt4 key购买 nike

我试图通过在我的主要 Activity 中使用切换按钮在“全亮度”和“手机正常亮度”之间切换。我使用这段代码成功地处理了亮度的切换:

switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if (bChecked) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
getWindow().setAttributes(params);
}
}
});

if (switchButton.isChecked()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
getWindow().setAttributes(params);
}

问题是,

切换到“全亮度”后,当我更改 Activity 时,亮度恢复正常。现在,如何跟踪“主要 Activity ”中的“亮度设置”并将其应用于应用的其他 Activity ?

注意 我不想更改系统亮度。亮度只会在使用该应用程序时发生变化。

提前致谢。

最佳答案

好的。终于解决了我的问题。

  1. 我创建了一个具有亮度控制逻辑的类,使用 SharedPreferences 来处理状态。代码:

     if (br.getString("set", "").equals("1")) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.screenBrightness = 1.0f;
    getWindow().setAttributes(params);
    } else {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    getWindow().setAttributes(params);
    }
  2. 然后使用 Activity 扩展类并在 onCreate 方法中初始化 SharedPreferences。

     br = getSharedPreferences("br", Activity.MODE_PRIVATE);

感谢@0X0nosugar 的想法。

关于android - 改变 Activity 后亮度恢复正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50879805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com