android判断是否是深色模式

判断系统是否是深色模式和判断当前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

结论
判断此app应用了何种模式

int uiMode = context.getResources().getConfiguration().uiMode
if ((uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES){}

判断系统应用了何种模式

UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
if (uiModeManager.getNightMode()==UiModeManager.MODE_NIGHT_YES) {}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容