I am looking for a way to enable night mode programmatically with an Android code:
public static void setNightMode(Context target, boolean state){
UiModeManager uiManager = (UiModeManager) target.getSystemService(Context.UI_MODE_SERVICE);
if (state) {
//uiManager.enableCarMode(0);
uiManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
} else {
// uiManager.disableCarMode(0);
uiManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
}
}
Nothing has changed on my screen, the night mode is still disables.
According to this link
There is no need to enable carMode or deskMode.
I have the following logcat on Android Studio:
11-26 12:15:16.662 3823-3823/? D/UiModeManager: updateConfigurationLocked: mDockState=0; mCarMode=false; mNightMode=2; uiMode=33
11-26 12:15:26.802 3823-3823/? V/UiModeManager: updateLocked: null action, mDockState=0, category=null
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…