文章目录一、概述二、品牌与型号方法详解三、系统版本方法详解四、设备类型与屏幕信息五、Demo 完整演示代码5.1 加载方法5.2 屏幕密度标签转换5.3 UI信息列表展示六、常见应用场景兼容性检查崩溃日志附加设备信息七、API 速查表一、概述近期发现一款很有意思的HarmonyOS 三方库, 地址 pura/harmony-utils(V1.4.0) , 作者是桃花镇童长老, 我这里也是直接通过该作者公布的源码进行案例编写进行,写了到目前写了一部分demo ,感觉确实很有帮助,这里呢也是开始写一个系列的演示demo 供大家参考。如有帮助可以在OpenHarmony中进行下载安装进行使用哦案例demo导航展示↓↓↓↓↓↓接下来言归正传 ↓↓↓↓DeviceUtil对 HarmonyOSdeviceInfo模块进行了封装提供了查询设备品牌、型号、系统版本等信息的便捷方法。这些信息在以下场景中非常有用崩溃日志上报附带设备环境信息功能兼容性检查根据系统版本决定是否开启特定功能用户支持快速识别用户设备型号设备类型适配手机/平板/PC 不同布局二、品牌与型号方法详解// DeviceUtil.ets工具类源码// 获取设备品牌名称如 HUAWEIstaticgetBrand():string{returndeviceInfo.brand;}// 获取认证型号如 ALN-AL00staticgetProductModel():string{returndeviceInfo.productModel;}// 获取设备品牌名称 认证型号如 HUAWEI ALN-AL00staticgetBrandModel():string{return${deviceInfo.brand}${deviceInfo.productModel};}// 获取外部产品系列名称市场名如 HUAWEI Mate 60 ProstaticgetMarketName():string{returndeviceInfo.marketName;}// 获取硬件版本号如 HL1CMSMstaticgetHardwareModel():string{returndeviceInfo.hardwareModel;}// 获取设备厂家名称如 HUAWEIstaticgetManufacture():string{returndeviceInfo.manufacture;}getBrandModel vs getMarketNamegetBrandModel()→HUAWEI ALN-AL00技术型号用于日志/调试getMarketName()→HUAWEI Mate 60 Pro市场名称适合展示给用户三、系统版本方法详解// DeviceUtil.ets工具类源码// 获取系统版本全称如 OpenHarmony-5.1.0.60staticgetOsFullName():string{returndeviceInfo.osFullName;}// 获取产品版本如 ALN-AL00 5.0.0.1(XXX)staticgetDisplayVersion():string{returndeviceInfo.displayVersion;}// 获取 OS 版本号Major.Senior.Feature如 5.0.4staticgetOsVersion():string{return${deviceInfo.majorVersion}.${deviceInfo.seniorVersion}.${deviceInfo.featureVersion};}// 获取 Build 版本号编译构建版本staticgetBuildVersion():number{returndeviceInfo.buildVersion;}// 获取系统软件 API 版本如 12staticgetSdkApiVersion():number{returndeviceInfo.sdkApiVersion;}// 获取 ABI 列表如 arm64-v8astaticgetAbiList():string{returndeviceInfo.abiList;}// 获取系统发布类型Canary、Beta、ReleasestaticgetOsReleaseType():string{returndeviceInfo.osReleaseType;}四、设备类型与屏幕信息// DeviceUtil.ets工具类源码// 获取当前设备类型枚举值staticgetDeviceType():number{returnDeviceUtil.getDeviceCapabilitySync().deviceType;}// 获取当前设备类型字符串staticgetDeviceTypeStr():string{letdeviceTypeDeviceUtil.getDeviceType();switch(deviceType){caseresourceManager.DeviceType.DEVICE_TYPE_PHONE:return手机;caseresourceManager.DeviceType.DEVICE_TYPE_TABLET:return平板;caseresourceManager.DeviceType.DEVICE_TYPE_PC:return电脑;caseresourceManager.DeviceType.DEVICE_TYPE_TV:return电视;caseresourceManager.DeviceType.DEVICE_TYPE_CAR:return汽车;caseresourceManager.DeviceType.DEVICE_TYPE_WEARABLE:return穿戴;caseresourceManager.DeviceType.DEVICE_TYPE_2IN1:return2IN1;default:return;}}// 获取当前屏幕方向0竖屏1横屏staticgetDirection():Number{returnDeviceUtil.getConfigurationSync().direction}// 获取屏幕密度staticgetScreenDensity():number{returnDeviceUtil.getDeviceCapabilitySync().screenDensity;}五、Demo 完整演示代码5.1 加载方法// DeviceUtilDemoPage.etsDemo 源码// 加载设备品牌信息loadDeviceInfo(){this.brandResultDeviceUtil.getBrand();this.modelResultDeviceUtil.getProductModel();constbrandModelDeviceUtil.getBrandModel();this.marketResultDeviceUtil.getMarketName();this.hardwareResultDeviceUtil.getHardwareModel();this.manufactureResultDeviceUtil.getManufacture();this.addLog(DeviceInfo,品牌型号:${brandModel},info);}// 加载系统版本信息loadSystemVersion(){this.osFullNameResultDeviceUtil.getOsFullName();this.displayVersionResultDeviceUtil.getDisplayVersion();this.buildVersionResultDeviceUtil.getBuildVersion().toString();this.sdkApiVersionResultDeviceUtil.getSdkApiVersion().toString();this.osVersionResultDeviceUtil.getOsVersion();this.abiListResultDeviceUtil.getAbiList();this.releaseTypeResultDeviceUtil.getOsReleaseType();this.addLog(System,系统全名:${this.osFullNameResult},info);}// 加载屏幕和设备类型loadScreenDevice(){this.deviceTypeResultDeviceUtil.getDeviceTypeStr();constdirDeviceUtil.getDirection();this.directionResultdir0?竖屏:横屏;this.screenDensityValueDeviceUtil.getScreenDensity();this.screenDensityResultthis.getScreenDensityLabel(this.screenDensityValue);this.addLog(Screen,设备类型:${this.deviceTypeResult}, 方向:${this.directionResult},info);}5.2 屏幕密度标签转换getScreenDensityLabel(value:number):string{switch(value){caseresourceManager.ScreenDensity.SCREEN_SDPI:returnSDPI (120);caseresourceManager.ScreenDensity.SCREEN_MDPI:returnMDPI (160);caseresourceManager.ScreenDensity.SCREEN_LDPI:returnLDPI (240);caseresourceManager.ScreenDensity.SCREEN_XLDPI:returnXLDPI (320);caseresourceManager.ScreenDensity.SCREEN_XXLDPI:returnXXLDPI (480);caseresourceManager.ScreenDensity.SCREEN_XXXLDPI:returnXXXLDPI (640);default:return未知 (${value});}}5.3 UI信息列表展示// Demo 中用 Builder buildInfoSection 复用信息展示组件// 品牌与型号this.buildInfoSection(品牌与型号,[{label:getBrand(),value:this.brandResult,highlight:true},{label:getProductModel(),value:this.modelResult},{label:getBrandModel(),value:DeviceUtil.getBrandModel()},{label:getMarketName(),value:this.marketResult},])// 硬件信息this.buildInfoSection(硬件信息,[{label:getHardwareModel(),value:this.hardwareResult},{label:getManufacture(),value:this.manufactureResult},])// 版本信息this.buildInfoSection(版本信息,[{label:getOsFullName(),value:this.osFullNameResult,highlight:true},{label:getDisplayVersion(),value:this.displayVersionResult},{label:getOsVersion(),value:this.osVersionResult},{label:getBuildVersion(),value:this.buildVersionResult},{label:getSdkApiVersion(),value:this.sdkApiVersionResult},])// Builder 组件实现BuilderbuildInfoSection(title:string,rows:InfoRow[]){Column(){Text(title).fontSize(13).fontColor(#666).fontWeight(FontWeight.Medium).alignSelf(ItemAlign.Start).margin({bottom:8})ForEach(rows,(row:InfoRow){Column(){Row(){Text(row.label).fontSize(11).fontColor(#888).fontFamily(monospace)Blank()Text(row.value).fontSize(12).fontColor(row.highlight?#4080FF:#1a1a1a).fontWeight(row.highlight?FontWeight.Bold:FontWeight.Normal)}.width(100%)}.width(100%).padding({top:6,bottom:6})},(row:InfoRow)row.label)}.width(100%).padding(14).backgroundColor(#FFFFFF).borderRadius(12)}六、常见应用场景兼容性检查// 根据 SDK 版本决定是否使用新 APIfunctionisApiSupported(minApiVersion:number):boolean{returnDeviceUtil.getSdkApiVersion()minApiVersion;}// 使用if(isApiSupported(14)){// 使用 API14 的新功能DisplayUtil.getPrimaryDisplaySync();}else{// 降级处理DisplayUtil.getDefaultDisplaySync();}崩溃日志附加设备信息functionbuildCrashReport(error:Error):string{return[设备:${DeviceUtil.getMarketName()},型号:${DeviceUtil.getBrandModel()},系统:${DeviceUtil.getOsFullName()},API:${DeviceUtil.getSdkApiVersion()},类型:${DeviceUtil.getDeviceTypeStr()},错误:${error.message},堆栈:${error.stack},].join(\n);}七、API 速查表方法返回类型示例值getBrand()string“HUAWEI”getProductModel()string“ALN-AL00”getBrandModel()string“HUAWEI ALN-AL00”getMarketName()string“HUAWEI Mate 60 Pro”getOsFullName()string“OpenHarmony-5.1.0.60”getOsVersion()string“5.0.4”getSdkApiVersion()number12getDeviceTypeStr()string“手机”getDirection()number0(竖屏) / 1(横屏)getScreenDensity()number480(XXLDPI)