📌 场景介绍
很多应用在初始化或诊断过程中需要读取设备基础信息,例如:
-
显示当前设备型号、系统版本
-
上报设备环境到后台(用于 bug 定位、运营分析)
-
区分设备品牌适配样式等
本篇基于 @ohos.deviceInfo
模块获取系统信息,并动态展示。
🧱 页面结构
/entry/src/main/ets
└── pages/
└── DeviceInfoDemo.ets // 设备信息展示页
🧩 DeviceInfoDemo.ets 示例页面
import deviceInfo from '@ohos.deviceInfo'
@Entry
@Component
struct DeviceInfoDemo {
@State brand: string = ''
@State model: string = ''
@State osVersion: string = ''
@State manufacturer: string = ''
aboutToAppear() {
this.getDeviceInfo()
}
private getDeviceInfo() {
try {
this.brand = deviceInfo.brand || ''
this.model = deviceInfo.model || ''
this.osVersion = deviceInfo.osFullName || ''
this.manufacturer = deviceInfo.manufacture || ''
} catch (err) {
console.error('获取设备信息失败:', JSON.stringify(err))
}
}
build() {
Column() {
Text("📱 当前设备信息").fontSize(22).margin({ bottom: 20 })
Text(`设备品牌:${this.brand}`).fontSize(16).margin(5)
Text(`设备型号:${this.model}`).fontSize(16).margin(5)
Text(`制造商:${this.manufacturer}`).fontSize(16).margin(5)
Text(`系统版本:${this.osVersion}`).fontSize(16).margin(5)
}
.padding(30)
.height('100%')
.alignItems(HorizontalAlign.Start)
}
}
✅ 效果说明
-
自动展示当前设备的品牌、型号、系统版本、制造商
-
无需用户授权
-
可拓展用于初始化打点、兼容判断、设备支持差异处理
🔧 拓展建议
-
获取唯一设备标识符(需读取权限,部分版本受限制)
-
展示屏幕尺寸、分辨率等 UI 适配信息
-
结合日志模块自动上报设备环境信息用于 bug 定位