SystemSettingUtil工具类

public class SystemSettingUtil {

    private static final int LOG_DOMAIN = 218107648;
    private static final String TAG = "SystemSettingUtil";
    private static final HiLogLabel LOG_LABEL = new HiLogLabel(0, LOG_DOMAIN, TAG);
    private static final String LOG_FORMAT = "%{public}s";

    private static final String GET_GLOBAL = "GET_global";
    private static final String GET_SECURE = "GET_secure";
    private static final String GET_SYSTEM = "GET_system";
    private static final String SETTING_VALUE = "value";
    private static final String SYSTEM_SETTING = "dataability:///settings";

    public static String getGlobal(String key) {
        return getSettingValueAsString(key, GET_GLOBAL);
    }

    public static String getSecure(String key) {
        return getSettingValueAsString(key, GET_SECURE);
    }

    public static String getSystem(String key) {
        return getSettingValueAsString(key, GET_SYSTEM);
    }

    public static String getSettingValueAsString(String key, String method) {
        if (TextTool.isNullOrEmpty(key)) {
            HiLog.warn(LOG_LABEL, LOG_FORMAT, "getSettingValueAsString -> key is null or empty");
            return "";
        } else if (TextTool.isNullOrEmpty(method)) {
            HiLog.warn(LOG_LABEL, LOG_FORMAT, "getSettingValueAsString -> method is null or empty");
            return "";
        } else {
            try {
                return DataAbilityHelper.creator(ContextUtil.getHapGlobalContext()).call(Uri.parse(SYSTEM_SETTING),
                        method, key, null).getString(SETTING_VALUE, "");
            } catch (IllegalArgumentException | IllegalStateException | DataAbilityRemoteException e) {
                HiLog.error(LOG_LABEL, LOG_FORMAT, "getSettingValueAsString -> Exception");
                return "";
            }
        }
    }
}

ContextUtil工具类

public class ContextUtil {
 
    private static Context sHapGlobalContext;
 
    public static Context getHapGlobalContext() {
        return sHapGlobalContext;
    }
 
    public static void setHapGlobalContext(Context context) {
        if (context != null) {
            sHapGlobalContext = context;
        }
    }
}

使用方法:

在MyApplication onInitialize调用ContextUtil.setHapGlobalContext(this)设置全局Context;

调用SystemSettingUtil.getXXX。

注:key常量可使用hdc shell执行以下命令获取。

settings list system

settings list secure

settings list global

代码里怎么区分用户是否调整了系统字体大小设置里【显示大小】?

https://developer.huawei.com/...

(出处: 论坛首页)

解决方案:

private static final String INIT_DPI = "init_dpi";
private static final String DISPLAY_DENSITY_FORCED = "display_density_forced";
SystemSettingUtil.getSecure(INIT_DPI);
SystemSettingUtil.getSecure(DISPLAY_DENSITY_FORCED);

display_density_forced为空即显示大小为默认,display_density_forced<init_dpi显示大小为小,display_density_forced>init_dpi显示大小为大,未修改过显示大小时两项都为空。

原文链接:https://developer.huawei.com/...
原作者:π


华为开发者论坛
352 声望56 粉丝

华为开发者论坛是一个为开发者提供信息传播、开发交流、技术分享的交流空间。开发者可以在此获取技术干货、华为源码开放、HMS最新活动等信息,欢迎大家来交流分享!