《卡片添加至桌面》二、@ohos.deviceInfo使用指南

发布时间:2026/7/15 4:33:03

《卡片添加至桌面》二、@ohos.deviceInfo使用指南 HarmonyOS ohos.deviceInfo设备信息使用指南摘要本文全面介绍 HarmonyOSohos.deviceInfo模块的 API 属性与使用方法通过完整代码示例演示如何获取设备名称、品牌、型号、系统版本等信息并结合应用卡片场景展示实战应用。效果一、模块概述ohos.deviceInfo是 HarmonyOS 基础服务模块属于kit.BasicServicesKit用于获取当前设备的硬件与系统信息。所有属性均为同步只读无需申请权限开发者可以直接在页面或卡片中读取设备信息。1.1 适用场景设备信息展示应用根据设备类型适配 UI 布局桌面卡片显示设备名称与品牌日志采集与数据分析1.2 模块归属属性值模块名ohos.deviceInfoKitkit.BasicServicesKit引入方式import { deviceInfo } from kit.BasicServicesKit二、核心 API 详解2.1 常用属性列表属性名类型说明示例值brandstring设备品牌HUAWEImarketNamestring产品市场名称Mate 60 ProproductModelstring设备型号ALN-AL80deviceTypestring设备类型phone/tablet/2in1osFullNamestring操作系统全称HarmonyOSosReleaseTypestring系统版本类型Release/BetadisplayVersionstring显示版本号5.0.0osBrandstring操作系统品牌HarmonyOShardwareProfilestring硬件配置信息-serialstring设备序列号-bootloaderVersionstringBootloader版本号-abiListstring[]支持的ABI列表[arm64-v8a]2.2 设备类型枚举deviceType属性返回字符串类型常见值如下值含义phone手机tablet平板2in1二合一设备tv电视wearable可穿戴设备car车载设备三、权限说明deviceInfo模块的所有属性均为系统级只读信息无需额外申请权限即可使用。注意部分敏感属性如serial在低权限应用中可能返回空字符串。四、基础示例设备信息展示卡片4.1 创建项目使用 DevEco Studio 新建 ArkTS 项目选择Empty Ability模板。4.2 编写页面代码在entry/src/main/ets/pages/Index.ets中编写如下代码import{deviceInfo}fromkit.BasicServicesKit;EntryComponentstruct DeviceInfoDemo{StatedeviceBrand:string;StatedeviceMarketName:string;StatedeviceModel:string;StatedeviceType:string;StateosName:string;StateosVersion:string;aboutToAppear():void{this.loadDeviceInfo();}loadDeviceInfo():void{// 直接读取设备信息属性this.deviceBranddeviceInfo.brand;this.deviceMarketNamedeviceInfo.marketName;this.deviceModeldeviceInfo.productModel;this.deviceTypethis.getDeviceTypeName(deviceInfo.deviceType);this.osNamedeviceInfo.osFullName;this.osVersiondeviceInfo.displayVersion;}// 设备类型映射为中文名称getDeviceTypeName(type:string):string{consttypeMap:Recordstring,string{phone:手机,tablet:平板,2in1:二合一设备,tv:电视,wearable:可穿戴设备,car:车载设备};returntypeMap[type]??type;}build(){Column({space:20}){Text(设备信息展示).fontSize(24).fontWeight(FontWeight.Bold)// 信息卡片Column({space:16}){this.InfoRow(品牌,this.deviceBrand)this.InfoRow(产品名称,this.deviceMarketName)this.InfoRow(设备型号,this.deviceModel)this.InfoRow(设备类型,this.deviceType)this.InfoRow(操作系统,this.osName)this.InfoRow(系统版本,this.osVersion)}.width(90%).padding(24).backgroundColor(Color.White).borderRadius(16).shadow({radius:8,color:#1A000000,offsetY:4})Button(刷新信息).fontSize(16).width(60%).onClick((){this.loadDeviceInfo();})}.width(100%).height(100%).justifyContent(FlexAlign.Center).backgroundColor(#F5F5F5)}BuilderInfoRow(label:string,value:string){Row(){Text(label).fontSize(14).fontColor(#999999).width(80)Text(value).fontSize(16).fontWeight(FontWeight.Medium).fontColor(#333333).layoutWeight(1)}.width(100%).height(40)}}4.3 运行效果页面将展示一个设备信息卡片包含品牌、产品名称、设备型号、设备类型、操作系统和系统版本。五、进阶根据设备类型动态适配布局利用deviceInfo.deviceType可以实现针对不同设备类型的 UI 适配import{deviceInfo}fromkit.BasicServicesKit;EntryComponentstruct AdaptiveLayoutDemo{StateisPhone:booleantrue;StateisTablet:booleanfalse;aboutToAppear():void{consttypedeviceInfo.deviceType;this.isPhonetypephone;this.isTablettypetablet||type2in1;}build(){if(this.isPhone){// 手机布局 - 垂直排列Column({space:16}){Text(手机端布局).fontSize(20)// 垂直堆叠的卡片this.CardItem(卡片 A,#4FC3F7)this.CardItem(卡片 B,#81C784)this.CardItem(卡片 C,#FFB74D)}.width(100%).height(100%).padding(16)}else{// 平板/二合一布局 - 水平排列Row({space:16}){this.CardItem(卡片 A,#4FC3F7)this.CardItem(卡片 B,#81C784)this.CardItem(卡片 C,#FFB74D)}.width(100%).height(100%).padding(16)}}BuilderCardItem(title:string,color:string){Column(){Text(title).fontSize(18).fontWeight(FontWeight.Medium).fontColor(Color.White)}.width(this.isPhone?100%:33%).height(120).backgroundColor(color).borderRadius(12).justifyContent(FlexAlign.Center)}}六、在应用卡片中使用设备信息在桌面卡片场景中deviceInfo数据通常由主应用获取后通过Preferences传递给FormExtensionAbility。6.1 主应用端获取并保存import{deviceInfo}fromkit.BasicServicesKit;import{preferences}fromkit.ArkData;// 获取设备信息letmarketName:stringdeviceInfo.marketName;letbrand:stringdeviceInfo.brand;// 保存到 Preferences 供卡片读取letprefspreferences.getPreferencesSync(context,{name:myStore});prefs.putSync(marketName,marketName);prefs.putSync(brand,brand);prefs.flush();6.2 FormExtensionAbility 端读取import{formBindingData,FormExtensionAbility}fromkit.FormKit;import{Want}fromkit.AbilityKit;import{preferences}fromkit.ArkData;exportdefaultclassMyFormAbilityextendsFormExtensionAbility{onAddForm(want:Want):formBindingData.FormBindingData{letformIdwant.parameters?.[ohos.extra.param.key.form_identity]asstring;letprefspreferences.getPreferencesSync(this.context,{name:myStore});letformData{formId:formId,marketName:prefs.getSync(marketName,未知设备)asstring,brand:prefs.getSync(brand,未知品牌)asstring};returnformBindingData.createFormBindingData(formData);}}6.3 卡片 UI 展示Componentstruct DeviceInfoWidgetCard{LocalStorageProp(marketName)marketName:string;LocalStorageProp(brand)brand:string;build(){Column({space:8}){Text(设备${this.marketName}).fontSize(14).fontColor(rgba(0,0,0,0.9))Text(品牌${this.brand}).fontSize(12).fontColor(rgba(0,0,0,0.6))}.width(100%).height(100%).padding(16).justifyContent(FlexAlign.Center)}}七、常见问题与注意事项7.1 marketName 和 productModel 的区别属性说明示例marketName产品市场名称面向消费者Mate 60 ProproductModel设备内部型号编码ALN-AL80在展示给用户时优先使用marketName在技术分析时可使用productModel。7.2 模拟器返回默认值模拟器上brand、marketName等属性可能返回默认值如Huawei、rk3568建议在真机上调试以获取真实数据。7.3 设备类型字符串可能扩展deviceType的返回值可能随 HarmonyOS 版本更新而扩展建议在代码中使用default分支处理未知类型。八、总结知识点内容模块导入import { deviceInfo } from kit.BasicServicesKit获取品牌deviceInfo.brand获取产品名deviceInfo.marketName获取设备类型deviceInfo.deviceType获取系统版本deviceInfo.displayVersion权限要求无需额外权限数据传递通过 Preferences FormExtensionAbility 传递到卡片ohos.deviceInfo是开发设备信息类应用的基础模块结合同步读取和Preferences持久化存储可以方便地在应用和桌面卡片中展示设备信息。

相关新闻