Android P - 在运行时检查系统主题(默认、浅色或深色)

标签 android android-9.0-pie

有没有办法在运行时检查系统主题? (见下图) enter image description here

最佳答案

编辑/注意:好吧,我做了更多研究,据我所知,这只是 OnePlus 6 (OxygenOs) 上的全局设置,难道 android P 没有获得暗模式吗?

所以我想出了一个 hacky 的方法来获得这个设置:

系统设置存储在:content://settings/system

在我的设备 (OxygenOs 6.x.x) 上,主题值存储在 oem_black_mode。 .

要获取此结果,我们可以通过 adb 执行以下命令:

adb shell content query --uri content://settings/system/oem_black_mode

这不需要手机被 root。

我已经为内容解析器创建了一个简单的包装器(请随意使用/修改):

import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;

public class SystemSettingsResolver {

    public static final String OEM_BLACK_MODE = "oem_black_mode";
    public static final String OEM_BLACK_MODE_ACCENT_COLOR = "oem_black_mode_accent_color";
    public static final String OEM_BLACK_MODE_ACCENT_COLOR_INDEX = "oem_black_mode_accent_color_index";


    private Context context;

    public SystemSettingsResolver(Context context) {
        this.context = context;
    }

    public int getInt(String setting) {
        ContentResolver resolver = context.getContentResolver();
        try {
            return Settings.System.getInt(resolver, setting);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public String getString(String setting) {
        ContentResolver resolver = context.getContentResolver();
        return Settings.System.getString(resolver, setting);
    }

    // extend with getFloat etc whatever is required for your app.
}

下面是如何使用这个包装器:

public int systemTheme() {
   /**
    * In my testing:
    * 0 = light
    * 1 = dark
    * 2 = default
   */
   SystemSettingsResolver resolver = SystemSettingsResolver(this); //pass context
   return resolver.getInt(SystemSettingsResolver.OEM_BLACK_MODE)
} 

关于Android P - 在运行时检查系统主题(默认、浅色或深色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51908573/

相关文章:

java - android.widget.FrameLayout$LayoutParams 无法转换为 android.widget.AbsListView$LayoutParams

android - 找不到为什么输出到 TextView 是 NaN

android - 权限拒绝:阅读android.support.v4.content.FileProvider uri ANDROID饼图

使用引用资源 ID 的声音 URI 时 Android 通知 channel 声音停止工作

Android Dialog Fragment 不会关闭

android - 如何使用 npx 创建新的 React Native 应用程序?

android - 隐藏软键盘在 Android 9.0 Pie 中不起作用

android - 带 DisplayCutout 的全屏应用程序

android - SQLite: No Such Table Error Android P问题

java - 如何从 recyclerview 保存所选项目的状态?