HarmonyOS ResUtil 设备配置查询:getConfiguration 和 getDeviceCapability 详解
文章目录背景方法总览getConfigurationSync / getConfiguration系统配置暗色模式适配实战多语言检测实战getDeviceCapabilitySync / getDeviceCapability设备能力screenDensity 值的含义isRawDir判断 rawfile 路径是否为目录ResUtil 方法完整速查写在最后背景近期发现一款很有意思的HarmonyOS 三方库, 地址 pura/harmony-utils(V1.4.0) , 作者是桃花镇童长老, 我这里也是直接通过该作者公布的源码进行案例编写进行,写了到目前写了一部分demo ,感觉确实很有帮助,这里呢也是开始写一个系列的演示demo 供大家参考。如有帮助可以在OpenHarmony中进行下载安装进行使用哦案例demo导航展示↓↓↓↓↓↓接下来言归正传 ↓↓↓↓做 HarmonyOS 应用有时候需要知道当前设备的一些配置信息用户设置的语言是什么当前是深色模式还是浅色模式屏幕文字方向是从左到右还是从右到左阿拉伯语场景屏幕像素密度是多少用于精确计算 px 和 vp 换算设备类型是手机、平板还是 2in1 电脑ResUtil提供了两个方法来查询这些信息。方法总览getConfigurationSync / getConfiguration系统配置getConfigurationSync返回一个Configuration对象包含locale当前语言区域比如zh-Hans-CN简体中文、en-USdirection文字方向0 从左到右1 从右到左colorMode颜色模式0 浅色1 深色this.Btn(getConfigurationSync() 获取设备配置,#16A085,(){this.tryLog(getConfigurationSync(),(){constcfgResUtil.getConfigurationSync();returnlocale${cfg.locale}direction${cfg.direction}colorMode${cfg.colorMode};});})实际运行结果示例localezh-Hans-CN direction0 colorMode0中文、从左到右、浅色模式异步版本this.Btn(getConfiguration() async,#148F77,(){ResUtil.getConfiguration().then(cfg{this.addLog(getConfiguration() locale${cfg.locale} colorMode${cfg.colorMode});}).catch((e:Error){this.addLog(Error:${e.message});});})暗色模式适配实战colorMode字段是做暗色模式适配的关键import{ResUtil}from../Utils/ResUtil;functionisDarkMode():boolean{constcfgResUtil.getConfigurationSync();returncfg.colorMode1;// 1 深色模式}// 根据模式选择颜色constbgColorisDarkMode()?#1A1A2E:#FFFFFF;consttextColorisDarkMode()?#E0E0E0:#333333;当然HarmonyOS 也有系统级的ohos.app.ability.ConfigurationConstant常量可以用colorMode 1这个判断是通用写法。多语言检测实战locale字段用于判断当前语言实现动态文案切换constcfgResUtil.getConfigurationSync();constlangcfg.locale.split(-)[0];// 取语言部分比如 zh、enif(langzh){console.log(当前是中文);}elseif(langen){console.log(当前是英文);}不过实际项目里更规范的做法是把多语言字符串放资源文件让系统自动切换而不是在代码里手动判断。locale更多用于日志记录和特殊的格式化场景。getDeviceCapabilitySync / getDeviceCapability设备能力getDeviceCapabilitySync返回DeviceCapability对象包含screenDensity屏幕像素密度DPIdeviceType设备类型this.Btn(getDeviceCapabilitySync() 获取设备能力,#1ABC9C,(){this.tryLog(getDeviceCapabilitySync(),(){constcapResUtil.getDeviceCapabilitySync();returnscreenDensity${cap.screenDensity}deviceType${cap.deviceType};});})实际运行结果示例screenDensity3 deviceType1320dpi手机异步版本this.Btn(getDeviceCapability() async,#17A589,(){ResUtil.getDeviceCapability().then(cap{this.addLog(getDeviceCapability() screenDensity${cap.screenDensity}deviceType${cap.deviceType});}).catch((e:Error){this.addLog(Error:${e.message});});})screenDensity 值的含义值含义典型 DPI 范围1SDPI约 120 dpi2MDPI约 160 dpi3LDPI约 240 dpi4XLDPI约 320 dpi6XXLDPI约 480 dpi大多数现代手机是 3LDPI或更高。平板和折叠屏设备可能有不同值。isRawDir判断 rawfile 路径是否为目录ResUtil还提供了isRawDir方法用于检查rawfile目录下的路径是否为目录this.Btn(isRawDir() 根路径,#E67E22,(){this.tryLog(isRawDir(),()ResUtil.isRawDir().toString());})this.Btn(isRawDir(not_exist) → false,#D35400,(){this.tryLog(isRawDir(not_exist),(){try{returnResUtil.isRawDir(not_exist).toString();}catch(e){return[Expected Error]${e};}});})rawfile是 HarmonyOS 里存放原始文件资源的目录resources/rawfile/适合放 JSON 配置文件、音频文件等不需要编译处理的原始文件。isRawDir()传空字符串表示检查根目录应该返回true。ResUtil 方法完整速查方法功能返回值getResourceManager()获取资源管理器ResourceManagergetStringSync($r)同步获取字符串$r引用stringgetStringByNameSync(name)同步获取字符串资源名stringgetStringValue($r)异步获取字符串PromisestringgetStringByName(name)异步获取字符串资源名PromisestringgetNumber($r)获取数字资源numbergetColorSync($r)同步获取颜色值numbergetColorByNameSync(name)同步获取颜色值资源名numbergetColor($r)异步获取颜色值PromisenumbergetMediaContentBase64Sync($r)同步获取图片 Base64stringgetMediaBase64ByNameSync(name)同步获取图片 Base64资源名stringgetMediaContentBase64($r)异步获取图片 Base64PromisestringgetMediaContentSync($r)同步获取图片字节Uint8ArraygetMediaByNameSync(name)同步获取图片字节资源名Uint8ArraygetConfigurationSync()同步获取设备配置ConfigurationgetConfiguration()异步获取设备配置PromiseConfigurationgetDeviceCapabilitySync()同步获取设备能力DeviceCapabilitygetDeviceCapability()异步获取设备能力PromiseDeviceCapabilityisRawDir(path)判断rawfile路径是否为目录boolean写在最后getConfigurationSync里的locale和colorMode是做好应用适配的关键入口。特别是暗色模式现在用户对这个需求越来越多提前做好适配体验会好很多。