HarmonyOS 鸿蒙Next应用中怎么判断当前的设备类型?

HarmonyOS 鸿蒙Next应用中怎么判断当前的设备类型? 能否根据屏幕比例判断?

8 回复
  • 优先使用 deviceType:进行设备大类区分(如手机、平板),这是最直接的方法。

  • 参考 https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-device-info

  • import { deviceInfo } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    
    // 获取设备类型
    let deviceTypeInfo: string = deviceInfo.deviceType;
    hilog.info(0x0000, 'testTag', 'Device type: %{public}s', deviceTypeInfo);
    
    // 根据设备类型执行不同逻辑
    if (deviceTypeInfo === 'phone') {
      // 手机设备特定处理
    } else if (deviceTypeInfo === 'tablet') {
      // 平板设备特定处理
    } else if (deviceTypeInfo === 'wearable') {
      // 智能穿戴设备特定处理
    }
    
  • 辅以屏幕比例和断点:在需要精细适配屏幕形态时(如折叠屏的不同折叠状态、横竖屏切换),结合屏幕比例和窗口断点实现动态布局。

  • 屏幕比例(宽高比)可以作为设备类型判断的补充,尤其适用于区分同一设备类型的不同状态(如折叠屏展开/折叠)或不同屏幕形态的设备。你可以通过 @kit.DisplayKitdisplay 模块获取屏幕尺寸,计算宽高比。

  • import { display } from '@kit.DisplayKit';
    
    // 获取默认显示设备的信息
    let defaultDisplay = display.getDefaultDisplaySync();
    let screenWidth = defaultDisplay.width;  // 屏幕宽度(像素)
    let screenHeight = defaultDisplay.height; // 屏幕高度(像素)
    let screenRatio = screenWidth / screenHeight; // 计算宽高比
    
    // 根据宽高比进行判断
    if (screenRatio > 1) {
      // 横屏比例
    } else {
      // 竖屏比例
    }
    

    通常基于窗口宽度(vp)划分,例如 xs(<320vp)、sm(320vp-599vp)、md(600vp-839vp)、lg(≥840vp)。

更多关于HarmonyOS 鸿蒙Next应用中怎么判断当前的设备类型?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


通过@ohos.deviceInfo接口的deviceType属性来获取设备类型

let currentDeviceType: string = deviceInfo.deviceType;
console.info('当前设备类型: ' + currentDeviceType);

// 根据设备类型执行不同操作
if (currentDeviceType === 'phone') {
    // 执行手机设备特定的逻辑,例如设置竖屏
    console.info('这是一部手机。');
} else if (currentDeviceType === 'tablet') {
    // 执行平板设备特定的逻辑,例如设置横屏
    console.info('这是一台平板。');
} else if (currentDeviceType === 'wearable') {
    console.info('这是一块手表。');
} else if (currentDeviceType === 'tv') {
    console.info('这是一台智慧屏。');
} else if (currentDeviceType === 'car') {
    console.info('这是一台车机。');
} else if (currentDeviceType === '2in1') {
    console.info('这是一台二合一设备。');
}

希望HarmonyOS能继续优化系统稳定性,减少崩溃和重启的情况。

学习了

尊敬的开发者,您好,

目前不能通过屏幕比例判断当前设备类型,不过可以利用[@ohos.deviceInfo](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-device-info)中的deviceType属性可以获取设备类型,具体请参考设备类型枚举。

示例代码如下:

import { deviceInfo } from '@kit.BasicServicesKit';

@Entry
@ComponentV2
struct Index {
  build() {
    Column({ space: 12 }) {
      Button('获取当前设备信息1')
        .onClick(() => {
          let currentDeviceType: string = deviceInfo.deviceType;
          console.info('当前设备类型: ' + currentDeviceType);
          // 根据设备类型执行不同操作
          if (currentDeviceType === 'phone') {
            // 执行手机设备特定的逻辑,例如设置竖屏
            console.info('这是一部手机。');
          } else if (currentDeviceType === 'tablet') {
            // 执行平板设备特定的逻辑,例如设置横屏
            console.info('这是一台平板。');
          } else if (currentDeviceType === 'wearable') {
            console.info('这是一块手表。');
          } else if (currentDeviceType === 'tv') {
            console.info('这是一台智慧屏。');
          } else if (currentDeviceType === 'car') {
            console.info('这是一台车机。');
          } else if (currentDeviceType === '2in1') {
            console.info('这是一台二合一设备。');
          }
        });
    }
    .constraintSize({ minHeight: '100%' })
    .justifyContent(FlexAlign.Start)
    .padding(16);
  }
}

如果还是不能解决您的问题,麻烦您提供如下信息:

请问您是在什么样的业务场景中使用该能力,交互流程是怎样的,在哪一个环节遇到了问题?方便说明能力不满足可能带来的影响:什么时间用到?是否高频?有无三方库可以做到?若提供该能力,是否会造成大工作量返工?请您注意提供的内容不要包含您或第三方的非公开信息,如给您带来不便,敬请谅解。

DeviceTypes

PhonePC/2in1TabletTVWearable

设备类型枚举值,可用于校验deviceType的返回值。

元服务API(仅ArkTS-Dyn):从API版本20开始,该接口支持在元服务中使用。

系统能力:SystemCapability.Startup.SystemInfo

名称 说明
TYPE_DEFAULT ‘default’ 默认设备。
TYPE_PHONE ‘phone’ 手机。
TYPE_TABLET ‘tablet’ 平板。
TYPE_2IN1 ‘2in1’ PC/2in1。
TYPE_TV ‘tv’ 智慧屏。
TYPE_WEARABLE ‘wearable’ 智能手表。
TYPE_CAR ‘car’ 车机。

在HarmonyOS Next中,通过import deviceInfo from '@ohos.deviceInfo'获取设备类型,deviceInfo.deviceType返回phonetablettvwearable等枚举值。也可使用systemCapability接口判断屏幕尺寸或传感器特征。

可以直接使用系统提供的 @ohos.deviceInfo 模块获取准确的设备类型(如手机、平板、智慧屏等),不推荐仅靠屏幕比例判断,因为不同设备的屏幕比例可能存在重叠,比例本身无法唯一决定设备类型。

import deviceInfo from '@ohos.deviceInfo';

let type = deviceInfo.deviceType;
// 例如:'phone'、'tablet'、'tv'、'car'、'smartVision' 等

如果因特殊需求必须用屏幕尺寸辅助判断,可通过 window.getLastWindow() 获取窗口宽高,再结合 display 获取物理尺寸,设定像素密度或英寸级阈值进行粗略分类,但这种做法可靠性较低。官方建议以 deviceInfo.deviceType 为准。

回到顶部