gpt4 book ai didi

android - 如何检测系统是否使用暗模式?

转载 作者:行者123 更新时间:2023-12-05 00:19:24 24 4
gpt4 key购买 nike

我正在使用这个简单的功能来获得暗模式。我在共享首选项中存储了一个 bool 值。如果值不存在,我默认返回 false。
这是我的简单代码:

public static boolean getNightMode(){
SharedPreferences pref = getApplicationContext().getSharedPreferences("nightMode", 0);
return pref.getBoolean("nightMode",false);
}
现在,我不想默认返回 false,而是想返回系统暗模式状态。
我的意思是如果系统使用暗模式,则返回 true。
我怎样才能做到这一点?

最佳答案

您可以使用配置设置。

int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're using the light theme
break;
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're using dark theme
break;
}
更多详情请引用 developer site .
Kotlin :
val currentNightMode = configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
when (currentNightMode) {
Configuration.UI_MODE_NIGHT_NO -> {} // Night mode is not active, we're using the light theme
Configuration.UI_MODE_NIGHT_YES -> {} // Night mode is active, we're using dark theme
}

关于android - 如何检测系统是否使用暗模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63027765/

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