[Harmony]获取设备参数
获取屏幕宽度/屏幕高度/状态栏高度/导航栏高度/刘海高度/设备型号/系统版本号...
DevicesUtil
import window from '@ohos.window';
import { common } from '@kit.AbilityKit';
import display from '@ohos.display';
import deviceInfo from '@ohos.deviceInfo';
import i18n from '@ohos.i18n';export class DevicesUtil {/// 物理像素转虚拟像素private static pxToVp(px: number): number {const dis = display.getDefaultDisplaySync();return px / (dis.densityDPI / 160); // 标准转换公式}/// 同步获取屏幕信息static getScreenDisplaySync(): display.Display {return display.getDefaultDisplaySync(); // 返回的宽度和高度单位为物理像素(px)}/// 获取屏幕宽度(vp)static getScreenWidthVP(): number {try {const displayInfo = display.getDefaultDisplaySync();return DevicesUtil.pxToVp(displayInfo.width);} catch {return 360; // 默认宽度(vp)}}/// 获取屏幕高度(vp)static getScreenHeightVP(): number {try {const displayInfo = display.getDefaultDisplaySync();return DevicesUtil.pxToVp(displayInfo.height);} catch {return 780; // 默认高度(vp)}}/// 状态栏高度static async getStatusBarHeight(context: common.UIAbilityContext, isVP: boolean = true): Promise<number> {try {const win = await window.getLastWindow(context);/*getWindowAvoidArea返回的物理像素,需要转换为虚拟像素返回,便于布局TYPE_SYSTEM:获取状态栏区域(推荐使用)TYPE_NAVIGATION_INDICATOR:获取导航栏区域TYPE_CUTOUT:获取刘海屏区域*/const avoidArea = await win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);if (isVP) {return DevicesUtil.pxToVp(avoidArea.topRect.height);} else {return avoidArea.topRect.height;}} catch {return isVP ? 24 : 96; // 默认安全高度 // 96 假设480dpi设备}}/// 底部导航栏高度/// 不包含状态栏高度,仅包含屏幕底部导航栏自身的像素高度static async getNavigationBarHeight(context: common.UIAbilityContext, isVP: boolean = true): Promise<number> {try {const win = await window.getLastWindow(context);const avoidArea = await win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);if (isVP) {return DevicesUtil.pxToVp(avoidArea.bottomRect.height);} else {return avoidArea.bottomRect.height;}} catch {return isVP ? 48 : 192; // 默认导航栏高度 // 192 假设480dpi设备}}/// 刘海屏高度/// 获取的 avoidArea.topRect.height 不包含导航栏高度,仅表示刘海屏(或挖孔屏)区域的高度// 特殊说明:刘海屏高度通常位于状态栏区域内部,可能与状态栏存在重叠区域static async getCutoutHeight(context: common.UIAbilityContext, isVP: boolean = true): Promise<number> {try {const win = await window.getLastWindow(context);const avoidArea = await win.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT);if (isVP) {return DevicesUtil.pxToVp(avoidArea.topRect.height);} else {return avoidArea.topRect.height;}} catch {return 0; // 刘海屏默认高度无论单位都返回0}}/// 获取设备类型 (如: "phone")static getDeviceType(): string {try {return deviceInfo.deviceType;} catch {return '';}}/// 获取设备显示型号(如:"emulator"、"P50 Pro")static getDisplayModel(): string {return deviceInfo.marketName || deviceInfo.productModel}/// 获取设备品牌(如:"HUAWEI")static getDeviceBrand(): string {return deviceInfo.brand}/// 获取系统版本号 (如: "OpenHarmony-5.0.1")static getSystemVersion(): string {try {return deviceInfo.osFullName;} catch {return '';}}/// 获取系统语言 (如: "zh-Hans-CN")static getSystemLanguage(): string {try {return i18n.System.getSystemLanguage();} catch {return 'en-US'; // 默认返回英文}}/// 获取格式化语言名称 (如: "简体中文")/// 获取语言显示名称 en/zh/fr...static getLanguageDisplayName(langCode: string): string {try {return i18n.System.getDisplayLanguage(langCode,DevicesUtil.getSystemLanguage())} catch {return langCode}}
}
其他设备信息
AI添加的注释,不保真。
deviceInfo {const deviceType: string; // 设备类型(如手机/平板/电视等)const manufacture: string; // 设备制造商全称(如"Huawei Technologies Co., Ltd")const brand: string; // 设备商业品牌名称(如"HUAWEI")const marketName: string; // 市场销售名称(如"Mate 60 Pro")const productSeries: string; // 产品系列名称(如"Mate系列")const productModel: string; // 设备认证型号(如"OHOS-AN00")const softwareModel: string; // 内部软件子型号(用于系统升级识别)const hardwareModel: string; // 硬件版本标识(如主板型号)const hardwareProfile: string; // 硬件配置概要(如摄像头/传感器组合)const serial: string; // 设备唯一序列号const bootloaderVersion: string; // Bootloader引导程序版本const abiList: string; // 支持的CPU架构列表(armeabi-v7a/arm64-v8a等)const securityPatchTag: string; // 安全补丁版本标记(格式YYYY-MM-DD)const displayVersion: string; // 用户可见的系统版本号(如"3.1.0")const incrementalVersion: string; // 增量版本号(用于小版本更新)const osReleaseType: string; // 系统发布类型(Release/Alpha/Beta等)const osFullName: string; // 完整系统名称(含版本信息)const majorVersion: number; // 主版本号(大版本迭代)const seniorVersion: number; // 次版本号(功能更新)const featureVersion: number; // 特性版本号(小功能更新)const buildVersion: number; // 构建版本号(每日构建编号)const sdkApiVersion: number; // 当前SDK API版本const firstApiVersion: number; // 设备初始支持的API版本const versionId: string; // 版本唯一标识符const buildType: string; // 构建类型(User/UserDebug/Eng等)const buildUser: string; // 构建系统用户名const buildHost: string; // 构建服务器主机名const buildTime: string; // 系统构建时间戳const buildRootHash: string; // 系统根哈希校验值const udid: string; // 设备唯一标识符(匿名化处理)const distributionOSName: string; // 发行版系统名称(商业定制版标识)const distributionOSVersion: string; // 发行版系统版本const distributionOSApiVersion: number; // 发行版API版本const distributionOSApiName: string; // 发行版API名称const distributionOSReleaseType: string; // 发行版发布类型const ODID: string; // 组织分发标识符(企业设备管理用)const diskSN: string; // 磁盘序列号(存储设备唯一标识)
}
使用示例
import { DevicesUtil } from '../utils/DevicesUtil';
import { common } from '@kit.AbilityKit';@Component
export struct CustomNavigationBar {@State statusBarHeight: number = 24 // 默认值(vp)private context = getContext(this) as common.UIAbilityContext;aboutToAppear() {DevicesUtil.getStatusBarHeight(this.context).then(height => {this.statusBarHeight = height})}build() {Column() {// 状态栏占位区域Row().width('100%').height(this.statusBarHeight).backgroundColor(Color.Green)}.onAppear(async ()=>{console.log(`statusBarHeight=${this.statusBarHeight}`)})}
}