To implement Android night mode using UiModeManager and enable car mode, but show a notification, this may not be very good

I want to implement night mode in my APP (manual switch for day and night mode), and I found out that I can use UiModeManager.setNightMode() and add some resources like values-night and drawable-night to archive it.

In the document before setNightMode() we need enableCarMode() .

Using the code as shown below may work, but a problem has occurred.

 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 displays a notification allowing the user to exit vehicle mode.

enter image description here

Do you have an idea to disable this notification?

Or it just means that this is not the best way to implement Android night mode. And turning on car mode will lead to any weird differences from my APP or my phone?

PS: I wonder why we need to turn on car mode before setting night mode. Is there any deep thought in this?

PPS: I already know that I can change the theme to switch between day and night mode. It should call this.recreate() and makes the screen flicker for a second.

PPPS: If UiModeManager.setNightMode and change theme are not the best way to implement night mode, what choice do I have?

Edit:

Method 1: UiModeManager.setNightMode

enter image description here

Method 2: change theme

enter image description here

Change again:

I think my idea was wrong. Unable to turn off notification, but enable car mode.

All I want is to implement night mode, for example, method 1, without setting something like desktop or car mode, without showing flicker.

Finally

Using UiModeManager.setNightMode and turning on car mode is not the best way to implement night mode. Because it affects Android 5.0 and above.

When the car mode is on and the APP is running, I pressed the button at home, something strange happened (Test in nexus 7 Android 5.1.1). As the picture below:

Launch Android Auto

Find the Android Auto button on your car's display to get started

enter image description here

Unfortunately, UiModeManager.setNightMode cannot be used if car mode is not required.

With the exception of car mode, this result is ideal, and for developers, it can simply make several folders such as drawable-night and values-night without changing the code too large. While the mode is changed, it sends a broadcast and switches the system configuration to the corresponding user interface mode.

Although there are so many advantages, this is the wrong way to night mode.

I still wonder why the car and night mode go so close together.

+6
source share
2 answers

Thanks to NightModeHelper , I complete this function.

In MainActivity.onCreate() initialize the NightModeHelper.

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNightModeHelper = new NightModeHelper(this, R.style.Theme_Idxyer_NoActionBar); } 

And somewhere you need to switch the mode, add the line below.

 mNightModeHelper.toggle(); 

In addition, values-night with color.xml will be required.

All of this can work as a method of changing themes, showing flicker.

If you don't like the flicker, you can try setContentView(R.layout.main) and view the items again. Remember to remove activity.recreate() in the NightModeHelper class.

But if MainActivity contains a fragment, you need to add a fragment again, and the fragment will flicker.

You can check out the demo on Github for source code.

---- Editing -----

Android Support Library 23.2 and later supports night mode.

+5
source

The docs for setNightMode explicitly indicate that it only works in Docking mode or in car mode.

If you can just β€œchange the subject,” however (I don’t know how to do this) and then use Activity#recreate() , should you not get similar results with what you are doing now? Documents for #recreate() state:

Explain this activity for the new instance. This results in essentially the same thread as when creating the Activity due to a configuration change - the current instance will go through lifecycle to onDestroy () and the new instance created after it.

When changing the configuration, the actions go through the full life cycle (onPause β†’ onStop β†’ onDestroy β†’ onCreate β†’ onStart β†’ onResume), so it should not generate more or less flicker than switching to night mode using the configuration change mechanism.

0
source

All Articles