设备设置更改时是否有办法更改为夜间模式

时间:2020-02-05 14:42:18

标签: android-night-mode appcompatdelegate

我正在寻找如何切换应用主题

  1. 每个应用的设置,用户可以选择是否使用夜间模式
userSettings.night_mode = [null | yes | no]
  1. 如果用户未指定,则使用设备的系统设置,即当系统设置更改为夜间模式时,应用程序将进入夜间模式;当系统设置更改为非夜间模式时,则应用应以非夜间模式显示。

对于#1。我相信当用户更改应用程序的设置时,以下内容已足够:

userSettings.night_mode?.let {
    val nightMode = if(userSettings.night_mode == yes) 
                        AppCompatDelegate.MODE_NIGHT_YES 
                    else 
                        AppCompatDelegate.MODE_NIGHT_NO

    AppCompatDelegate.setDefaultNightMode(userSettings_night_mode)
}

并检查应用当前处于哪种模式:

int currentNightMode = getResources().getConfiguration().uiMode
        & Configuration.UI_MODE_NIGHT_MASK

但是对于#2来说,尽管有一个MODE_NIGHT_FOLLOW_SYSTEM并且可以做到,但还没有找到一种可靠的方法来处理系统

if (userSettings.night_mode == null) {
   AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}

其中uses the system's night mode setting to determine if it is night or not.,但似乎每个Chris Banes

Android Q onwards has a system night mode which can be enabled in the Settings app. 
Android Pie also has a system night mode setting, but it is only surfaced in the device’s ‘developer options’. 
For ease, I recommend treating Android Pie the same as previous versions of Android.

那么对于Android Q之前的操作系统,如何使应用程序的夜间模式遵循系统的夜间模式设置? (用户可以在Q之前的装有Android操作系统的设备上设置night mode吗?)

0 个答案:

没有答案