[Harmony]获取设备参数

获取屏幕宽度/屏幕高度/状态栏高度/导航栏高度/刘海高度/设备型号/系统版本号...

DevicesUtil

TypeScript 复制代码
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添加的注释,不保真。

TypeScript 复制代码
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; // 磁盘序列号(存储设备唯一标识)
}

使用示例

TypeScript 复制代码
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}`)
    })
  }
}
相关推荐
前端不太难5 小时前
从单页面到系统化:鸿蒙 App 演进路径
华为·状态模式·harmonyos
想你依然心痛7 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“文思智脑“——PC端AI智能体沉浸式智能写作工作台
人工智能·ar·harmonyos·ai写作
小雨青年7 小时前
鸿蒙 HarmonyOS 6 | Pura X Max 鸿蒙原生适配 09:展开态列表增加字段但不变复杂
华为·harmonyos
richard_yuu7 小时前
鸿蒙治愈游戏模块实战|四大轻量解压游戏、ArkTS动画交互与低功耗落地
游戏·交互·harmonyos
阿钱真强道11 小时前
24 鸿蒙LiteOS GPIO中断实战:从原理到上升沿/下降沿详解
harmonyos·中断·rk·liteos·开源鸿蒙·瑞芯微·rk2206
cd_9492172113 小时前
鸿蒙系统下抖音存储空间不足怎么办?缓存清理教程
缓存·华为·harmonyos
轻口味16 小时前
HarmonyOS 6.1 全栈实战录 - 14 渲染树透镜:FrameNode 渲染状态感知与高性能 UI 调优实战
ui·华为·harmonyos
HwJack2016 小时前
HarmonyOS NEXT 游戏APP开发中如何正确拦截退出手势
游戏·华为·harmonyos
HwJack2016 小时前
HarmonyOS APP开发中ArkTS/JS 类型错误全景拆解
javascript·华为·harmonyos
lqj_本人17 小时前
鸿蒙PC:鸿蒙版本 Electron 框架环境搭建并且实现 XH 笔记应用
笔记·electron·harmonyos