I want to implement night mode in my APP (manually switch day and night mode), and I learned that I can use UiModeManager.setNightMode()
and add some resources like values-night
and drawable-night
to archive it.
In document, before setNightMode()
, we need to enableCarMode()
.
Using code like below can work, but problem appeared.
UiModeManager uiManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
if (isNightMode) {
uiManager.enableCarMode(0);
uiManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
} else {
uiManager.disableCarMode(0);
uiManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
}
It shows a notification to allow user to exit car mode.
Do you have any idea to disable this notification?
Or these just means it is not the best way to implement android night mode. And enabling car mode would make any strange difference to my APP or my phone?
PS: I wonder that why we need to enable car mode before setting night mode. Is there some deep consideration in this?
PPS: I already know that I can change theme to switch day and night mode. It needs to call this.recreate()
and causes screen to flicker for a second.
PPPS: If UiModeManager.setNightMode
and change theme
are neither best way to implement night mode, what else choice do I have?
Edit:
Method 1: UiModeManager.setNightMode
Method 2: change theme
Edit again:
I think my idea was wrong. It is meaningless to disable the notification, but allow the car mode.
All I want is to implement night mode like Method 1, without setting something like desk or car mode, without showing flicker.
Finally
Using UiModeManager.setNightMode
and enabling car mode are not the good way to implement night mode. Because it makes some effect in Android 5.0 and above.
When car mode is enabled and APP is running, I pressed home button, something strange happened (Test in nexus 7 Android 5.1.1). As the picture showed below:
Launch Android Auto
Look for the Android Auto button on your car's display to start
Unfortunately, UiModeManager.setNightMode
can't be used unless car mode is needed.
Except car mode is enabled, the result is perfect and for developers it can just make some folders like drawable-night
and values-night
without change too much code. While mode is changed, it sends broadcast and switches the system configuration to the appropriate UI mode.
Although there are so many benefits, it is the wrong way to night mode.
I still wonder why car mode and night mode is combined so closely.