写点什么

开源一夏|OpenHarmony 如何查询设备类型(eTS)

作者:坚果
  • 2022 年 8 月 05 日
  • 本文字数:3006 字

    阅读完需:约 10 分钟

在应用开发过程中查询设备类型。


  • 通过 js 接口查询指定系统参数(const.build.characteristics)进而确定设备类型,详见系统属性


// @ts-nocheck
import parameter from '@ohos.systemParameter'
@Entry@Componentstruct GetDeviceTypeSample { @State deviceType: string = 'unknown';


build() { Column() { Text("获取设备类型").fontSize(24) Text(this.deviceType).fontSize(24).onClick(()=>{ try { this.deviceType = parameter.getSync("const.build.characteristics"); } catch(e) { console.log("getSync unexpected error: " + e); }
}) } .width('100%') .height('100%') }}
复制代码


通过 deviceInfo 查询设备类型,deviceInfo 中各个字段的含义请参考设备信息


// @ts-nocheck/* * Copyright (c) 2021 JianGuo Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *    http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */import parameter from '@ohos.systemParameter'
@Entry@Componentstruct GetDeviceTypeSample { @State deviceType: string = 'unknown';


build() { Column() { Text("获取设备类型").fontSize(24) //通过js接口查询指定系统参数(const.build.characteristics)进而确定设备类型 Text(this.deviceType).fontSize(24).onClick(()=>{ try { this.deviceType = parameter.getSync("const.build.characteristics"); } catch(e) { console.log("getSync unexpected error: " + e); }
}) //通过deviceInfo查询设备类型 Text(this.deviceType).fontSize(24).onClick(()=>{ try { this.deviceType= deviceInfo.deviceType; } catch(e) { console.log("getSync unexpected error: " + e); }
}) //通过display查询显示设备的属性(包括屏幕宽、高和屏幕密度等) Text(this.deviceType).fontSize(24).onClick(()=>{ display.getDefaultDisplay() .then((displayInfo) => { console.info('Display width: '+ displayInfo.width); console.info('Display height: '+ displayInfo.height); console.info('Display density: '+ displayInfo.densityDPI); }) .catch((error) => { console.error('Failed to obtain the default display size. Cause: '+JSON.stringify(error)); })
}) } .width('100%') .height('100%') }}
复制代码

如何查询屏幕/窗口尺寸

在应用开发过程中,为了在不同的设备上取得更好的显示效果,开发者可能需要查询屏幕尺寸或应用显示窗口尺寸。


  • 通过 display 查询显示设备的属性(包括屏幕宽、高和屏幕密度等),详见屏幕属性


// @ts-nocheck/* * Copyright (c) 2021 JianGuo Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *    http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */import deviceInfo from'@ohos.deviceInfo'import parameter from '@ohos.systemParameter'import display from '@ohos.display';
@Entry@Componentstruct GetDeviceTypeSample { @State deviceType: string = 'deviceType'; @State device: string = 'device'; @State displayInfo: string = 'displayInfo';
aboutToAppear() { try { this.deviceType = parameter.getSync("const.build.characteristics"); } catch(e) { console.log("getSync unexpected error: " + e); } } build() { Column() { Text("设备属性").fontSize(36) //通过js接口查询指定系统参数(const.build.characteristics)进而确定设备类型 Text(this.deviceType).fontSize(28).onClick(() => { try { this.deviceType = parameter.getSync("const.build.characteristics"); console.log("坚果" + this.deviceType); } catch (e) { console.log("getSync unexpected error: " + e); }
}) //通过deviceInfo查询设备类型 Text( this.device).fontSize(28).onClick(() => { this.device= deviceInfo.deviceType; }) //通过display查询显示设备的属性(包括屏幕宽、高和屏幕密度等) Text(this.displayInfo).fontSize(28).onClick(() => { display.getDefaultDisplay() .then((displayInfo) => { console.info('Display width: ' + displayInfo.width); console.info('Display height: ' + displayInfo.height); console.info('Display density: ' + displayInfo.densityDPI); this.displayInfo=JSON.stringify(displayInfo); console.info('Display density: ' + JSON.stringify(displayInfo));
}) .catch((error) => { console.error('Failed to obtain the default display size. Cause: ' + JSON.stringify(error)); })
}) } .width('100%') .height('100%').justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center) }}
复制代码


运行之后,在控制台打印


{"alive":true,"densityDPI":560,"densityPixels":3.5,"height":2560,"id":0,"name":"内置屏幕","refreshRate":60.000004,"rotation":0,"scaledDensity":3.5,"state":2,"width":1440,"xDPI":560,"yDPI":560}
复制代码


下面是参数描述

Display

描述 display 对象的属性。


系统能力: 以下各项对应的系统能力均为 SystemCapability.WindowManager.WindowManager.Core。



屏幕属性


https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/key-features/multi-device-app-dev/faqs.md#/openharmony/docs/blob/master/zh-cn/application-dev/ability/stage-ability.md

发布于: 刚刚阅读数: 3
用户头像

坚果

关注

此间若无火炬,我便是唯一的光 2020.10.25 加入

公众号:“大前端之旅”,华为云享专家,InfoQ签约作者,51CTO博客首席体验官,专注于大前端技术的分享,包括Flutter,小程序,安卓,VUE,JavaScript。

评论

发布
暂无评论
开源一夏|OpenHarmony如何查询设备类型(eTS)_开源_坚果_InfoQ写作社区