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) {}

你可能感兴趣的:(android判断是否是深色模式)