当前位置: 首页 > ds >正文

[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}`)})}
}

http://www.xdnf.cn/news/7665.html

相关文章:

  • SpringBoot 商城系统高并发引起的库存超卖库存问题 乐观锁 悲观锁 抢购 商品秒杀 高并发
  • 机械安全标准示例说明
  • 机器学习算法-聚类K-Means
  • 园区无人机智能巡检项目方案
  • 机器学习自然语言处理
  • SAP ECC即将停止支持,CIO如何应对S/4HANA的迁移挑战?
  • 【机器学习】logistic回归
  • Java 02入门,封装继承多态
  • 随记1-LLM多轮对话的陷阱
  • 操作系统学习笔记第5章 (竟成)
  • [解决方案] Word转PDF
  • Android SharedPreferences:从零到一的全面解析与实战指南
  • win10使用nginx做简单负载均衡测试
  • 省赛备考~全国青少年信息素养大赛-图形化编程复赛/省赛-模拟题-判断质数合数
  • JavaScript 数组方法详解:全面指南
  • 如何优化前端应用中的JavaScript执行效率?
  • 【LinkedList demo 内部类讲说】
  • BI是什么意思?一文讲清BI的概念与应用!
  • LeetCode-前缀和-和为K的子数组
  • 网络学习中通信方面的相关知识、及再次解读B=2W
  • 如果电路教材这么讲--积分运算电路中反馈电容并联电阻的作用
  • 制造业或跨境电商相关行业三种模式:OEM、ODM、OBM
  • 十大排序算法--快速排序
  • VitePress 中以中文字符结尾的字体加粗 Markdown 格式无法解析
  • 颠覆传统:PROFINET转EthernetIP在油墨生产线的成功应用
  • 小土堆pytorch--神经网路-卷积层池化层
  • 时尚外观+专业性能丨特伦斯V30Pro重新定义便携电子钢琴
  • 深入剖析Zynq AMP模式下CPU1中断响应机制:从原理到创新实践
  • 【八股战神篇】Java虚拟机(JVM)高频面试题
  • Spring Validation校验