android判断深色模式的方法

android10以后的版本才完全支持深色模式,测试下面两种方法判断系统是否深色模式都是有效的。

    public static boolean isDarkMode1() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            return false;
        }
        Context context = LauncherApplicationAgent.getInstance().getApplicationContext().getApplicationContext();
        UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
        return uiModeManager.getNightMode()==UiModeManager.MODE_NIGHT_YES;
    }

    public static boolean isDarkMode2(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            return false;
        }
        int nightModeFlags = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        boolean isDarkMode = nightModeFlags == Configuration.UI_MODE_NIGHT_YES;
        return isDarkMode;
    }

你可能感兴趣的:(android,java,开发语言)