android判断是否是深色模式

本文探讨了如何通过代码判断系统深色模式与应用自身设定的区别,展示了`UiModeManager`在检测系统和应用模式上的差异。开发者应关注app代码与系统全局设置的协调。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

判断系统是否是深色模式和判断当前app是否是深色模式不一样。

系统是深色模式,但此app自己代码单独设置了不是深色模式,
(用下列代码设置成非深色模式

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);


此时

    UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
    int uiMode = context.getResources().getConfiguration().uiMode
    Log.d("TEST","uiMode = "+Integer.toHexString(uiMode)+" H");
    Log.d("TEST","getNightMode() = "+Integer.toHexString(uiModeManager.getNightMode() )+" H");
    if ((uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES) {
        Log.d("TEST","night");
    } else {
       Log.d("TEST","not night");
    }
    if (uiModeManager.getNightMode()==UiModeManager.MODE_NIGHT_YES) {
        Log.d("TEST","night");
    } else {
        Log.d("TEST","not night");
    }

log:

D TEST   : uiMode = 11 H
D TEST   : getNightMode() = 2 H
D TEST   : not night
D TEST   : night
结论
int uiMode = context.getResources().getConfiguration().uiMode
if ((uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES){}

判断此app应用了何种模式。

UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
if (uiModeManager.getNightMode()==UiModeManager.MODE_NIGHT_YES) {}

判断系统应用了何种模式

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值