Android P-在运行时检查系统主题(默认,亮或暗)

时间:2018-08-18 12:16:47

标签: android android-9.0-pie

有没有一种方法可以在运行时检查系统主题? (见下图) enter image description here

1 个答案:

答案 0 :(得分:0)

编辑/注意:好的,据我所知,这只是OnePlus 6(OxygenOs)上的一个全局设置,我做了一些进一步的研究,毕竟Android P不会获得暗模式吗? strong>

所以我想出一种方法来获取此设置:

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

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

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

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

这不需要将手机植根。

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

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