SystemSettingUtil tools
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 ""; }}}}Copy the code
ContextUtil tools
public class ContextUtil { private static Context sHapGlobalContext; public static Context getHapGlobalContext() { return sHapGlobalContext; } public static void setHapGlobalContext(Context context) { if (context != null) { sHapGlobalContext = context; } }}
Copy the code
Usage:
-
In MyApplication onInitialize call ContextUtil. SetHapGlobalContext (this) set the global Context;
-
Call SystemSettingUtil getXXX.
Note: The key constant can be obtained by running the HDC shell command.
settings list system
settings list secure
settings list global
In the code how to distinguish whether the user has adjusted the system font size Settings [display size]?
Developer.huawei.com/consumer/cn…
(Source: Forum home page)
Solution:
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);Copy the code
If display_density_forced is null, the display size is the default. Display_density_forced <init_dpi Displays a small size. Display_density_forced >init_dpi displays a large size. Both entries are null if the display size is not modified.
The original link: developer.huawei.com/consumer/cn…
Original author: Shi Yong