要实现Android夜间模式,请使用UiModeManager并启用汽车模式,但显示通知,这可能不是一个好方法 [英] To implement Android night mode, using UiModeManager and enable car mode, but show a notification, it may not the good way

查看:94
本文介绍了要实现Android夜间模式,请使用UiModeManager并启用汽车模式,但显示通知,这可能不是一个好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的APP中实现夜间模式(手动切换白天和夜间模式),我了解到可以使用 UiModeManager.setNightMode()并添加一些资源,例如 values-夜晚 drawable-night 进行存档.

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.

在文档中,在 setNightMode()之前,我们需要 enableCarMode().

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?

或者这些只是意味着它不是实现android night模式的最佳方法.启用汽车模式会对我的APP或手机产生任何奇怪的改变吗?

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:我想知道为什么我们需要在设置夜间模式之前启用汽车模式.在这方面需要深思吗?

PS: I wonder that why we need to enable car mode before setting night mode. Is there some deep consideration in this?

PPS:我已经知道我可以更改主题以切换日夜模式.它需要调用 this.recreate()并使屏幕闪烁一秒钟.

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:如果 UiModeManager.setNightMode change theme 都不是实现夜间模式的最佳方法,我还有什么选择?

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

方法1: UiModeManager.setNightMode

方法2:更改主题

再次

我认为我的想法是错误的.禁用通知是没有意义的,但允许汽车模式.

I think my idea was wrong. It is meaningless to disable the notification, but allow the car mode.

我要实现的是像方法1那样的夜间模式,而无需设置桌面或汽车模式之类的东西,而不会出现闪烁.

All I want is to implement night mode like Method 1, without setting something like desk or car mode, without showing flicker.

最后

使用 UiModeManager.setNightMode 并启用汽车模式不是实现夜间模式的好方法.因为它在Android 5.0及更高版本中具有一定的作用.

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.

启用汽车模式并运行APP时,我按下了主页按钮,发生了一些奇怪的事情(在nexus 7 Android 5.1.1中进行测试).如下图所示:

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:

启动Android Auto

Launch Android Auto

在汽车显示屏上寻找Android Auto按钮以启动

Look for the Android Auto button on your car's display to start

不幸的是,除非需要汽车模式,否则无法使用 UiModeManager.setNightMode .

Unfortunately, UiModeManager.setNightMode can't be used unless car mode is needed.

除了启用汽车模式外,结果是完美的,对于开发人员来说,只需创建一些文件夹,如 drawable-night values-night ,而无需更改太多代码.更改模式后,它将发送广播,并将系统配置切换到适当的UI模式.

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.

尽管有很多好处,但这是夜间模式的错误方法.

我仍然想知道为什么汽车模式和夜间模式如此紧密地结合在一起.

I still wonder why car mode and night mode is combined so closely.

推荐答案

感谢 NightModeHelper ,我完成了此功能.

Thanks to NightModeHelper, I complete this feature.

MainActivity.onCreate()中,初始化NightModeHelper.

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 in somewhere you need to switch mode, add below line.

mNightModeHelper.toggle();

此外,将需要具有 color.xml values-night .

所有这些都可以像更改主题方法一样工作,显示闪烁.

All this can work like change theme method, showing flicker.

如果您不喜欢闪烁,则可以尝试 setContentView(R.layout.main)并再次初始化视图.不要忘记在NightModeHelper类中删除 activity.recreate().

If you do not like flicker, you can try to setContentView(R.layout.main) and init views again. Don't forget to delete activity.recreate() in NightModeHelper class.

但是,如果 MainActivity 包含片段,则需要再次添加片段,片段会闪烁.

But if MainActivity contains fragment, you need to add fragment again, and fragment would flicker.

您可以在Github中检查演示以获取源代码.

You can check demo in Github for source code.

----编辑-----

----Edit-----

在Android支持库23.2及更高版本中,官方支持夜间模式.

In Android Support Library 23.2 and above, Night Mode is official supported.

这篇关于要实现Android夜间模式,请使用UiModeManager并启用汽车模式,但显示通知,这可能不是一个好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆