当前位置:网站首页>How OpenHarmony Query Device Type
How OpenHarmony Query Device Type
2022-08-05 10:49:00 【Huawei cloud】
在应用开发过程中查询设备类型.
通过js接口查询指定系统参数(const.build.characteristics)进而确定设备类型,详见.
// @ts-nocheckimport parameter from '@ohos.systemParameter'@[email protected] 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'@[email protected] 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';@[email protected] 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.
名称 | 参数类型 | 可读 | 可写 | 说明 |
---|---|---|---|---|
id | number | 是 | 否 | 显示设备的id号. |
name | string | 是 | 否 | 显示设备的名称. |
alive | boolean | 是 | 否 | Shows whether the device is enabled. |
state | 是 | 否 | Displays the status of the device. | |
refreshRate | number | 是 | 否 | 显示设备的刷新率. |
rotation | number | 是 | 否 | Displays the screen rotation angle of the device. |
width | number | 是 | 否 | Displays the width of the device,单位为像素. |
height | number | 是 | 否 | Displays the height of the device,单位为像素. |
densityDPI | number | 是 | 否 | Displays the screen density of the device,单位为DPI. |
densityPixels | number | 是 | 否 | Displays the screen density of the device,单位为像素. |
scaledDensity | number | 是 | 否 | The scaling factor of the display device's display font. |
xDPI | number | 是 | 否 | xThe exact physical pixel value per inch of screen in the orientation. |
yDPI | number | 是 | 否 | yThe exact physical pixel value per inch of screen in the orientation. |
设备信息
说明: 本模块首批接口从API version 6开始支持.后续版本的新增接口,采用上角标单独标记接口的起始版本.
导入模块
import deviceInfo from '@ohos.deviceInfo'
属性
系统能力:以下各项对应的系统能力均为SystemCapability.Startup.SysInfo. 权限:The permissions required for each of the following vary,详见下表.
名称 | 参数类型 | 可读 | 可写 | 描述 |
---|---|---|---|---|
deviceType | string | 是 | 否 | 设备类型. |
manufacture | string | 是 | 否 | 设备厂家名称. |
brand | string | 是 | 否 | Device brand name. |
marketName | string | 是 | 否 | External product line. |
productSeries | string | 是 | 否 | 产品系列. |
productModel | string | 是 | 否 | 认证型号. |
softwareModel | string | 是 | 否 | Internal software submodel. |
hardwareModel | string | 是 | 否 | 硬件版本号. |
hardwareProfile | string | 是 | 否 | 硬件Profile. |
serial | string | 是 | 否 | 设备序列号. 需要权限:ohos.permission.sec.ACCESS_UDID,This permission is a system permission |
bootloaderVersion | string | 是 | 否 | Bootloader版本号. |
abiList | string | 是 | 否 | 应用二进制接口(Abi)列表. |
securityPatchTag | string | 是 | 否 | 安全补丁级别. |
displayVersion | string | 是 | 否 | 产品版本. |
incrementalVersion | string | 是 | 否 | 差异版本号. |
osReleaseType | string | 是 | 否 | The release type of the system,取值为: - Canary:Early preview releases for specific developers,不承诺API稳定性. - Beta:Publicly released to developersBeta版本,不承诺API稳定性. - Release:The official version that is publicly released to developers,承诺API稳定性. |
osFullName | string | 是 | 否 | 系统版本. |
majorVersion | number | 是 | 否 | Major版本号,Added with major version updates. |
seniorVersion | number | 是 | 否 | Senior版本号,with local architecture、Significant feature additions. |
featureVersion | number | 是 | 否 | Feature版本号,Identifies the planned new feature release. |
buildVersion | number | 是 | 否 | Build版本号,Identifies the version number of the compiled build. |
sdkApiVersion | number | 是 | 否 | 系统软件API版本. |
firstApiVersion | number | 是 | 否 | The first version of system softwareAPI版本. |
versionId | string | 是 | 否 | 版本ID. |
buildType | string | 是 | 否 | 构建类型. |
buildUser | string | 是 | 否 | 构建用户. |
buildHost | string | 是 | 否 | 构建主机. |
buildTime | string | 是 | 否 | 构建时间. |
buildRootHash | string | 是 | 否 | 构建版本Hash. |
udid7+ | string | 是 | 否 | 设备Udid. 需要权限:ohos.permission.sec.ACCESS_UDID,This permission is a system permission |
在应用开发过程中查询设备类型.
通过js接口查询指定系统参数(const.build.characteristics)进而确定设备类型,详见.
// @ts-nocheckimport parameter from '@ohos.systemParameter'@[email protected] 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'@[email protected] 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';@[email protected] 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.
名称 | 参数类型 | 可读 | 可写 | 说明 |
---|---|---|---|---|
id | number | 是 | 否 | 显示设备的id号. |
name | string | 是 | 否 | 显示设备的名称. |
alive | boolean | 是 | 否 | Shows whether the device is enabled. |
state | 是 | 否 | Displays the status of the device. | |
refreshRate | number | 是 | 否 | 显示设备的刷新率. |
rotation | number | 是 | 否 | Displays the screen rotation angle of the device. |
width | number | 是 | 否 | Displays the width of the device,单位为像素. |
height | number | 是 | 否 | Displays the height of the device,单位为像素. |
densityDPI | number | 是 | 否 | Displays the screen density of the device,单位为DPI. |
densityPixels | number | 是 | 否 | Displays the screen density of the device,单位为像素. |
scaledDensity | number | 是 | 否 | The scaling factor of the display device's display font. |
xDPI | number | 是 | 否 | xThe exact physical pixel value per inch of screen in the orientation. |
yDPI | number | 是 | 否 | yThe exact physical pixel value per inch of screen in the orientation. |
设备信息
说明: 本模块首批接口从API version 6开始支持.后续版本的新增接口,采用上角标单独标记接口的起始版本.
导入模块
import deviceInfo from '@ohos.deviceInfo'
属性
系统能力:以下各项对应的系统能力均为SystemCapability.Startup.SysInfo. 权限:The permissions required for each of the following vary,详见下表.
名称 | 参数类型 | 可读 | 可写 | 描述 |
---|---|---|---|---|
deviceType | string | 是 | 否 | 设备类型. |
manufacture | string | 是 | 否 | 设备厂家名称. |
brand | string | 是 | 否 | Device brand name. |
marketName | string | 是 | 否 | External product line. |
productSeries | string | 是 | 否 | 产品系列. |
productModel | string | 是 | 否 | 认证型号. |
softwareModel | string | 是 | 否 | Internal software submodel. |
hardwareModel | string | 是 | 否 | 硬件版本号. |
hardwareProfile | string | 是 | 否 | 硬件Profile. |
serial | string | 是 | 否 | 设备序列号. 需要权限:ohos.permission.sec.ACCESS_UDID,This permission is a system permission |
bootloaderVersion | string | 是 | 否 | Bootloader版本号. |
abiList | string | 是 | 否 | 应用二进制接口(Abi)列表. |
securityPatchTag | string | 是 | 否 | 安全补丁级别. |
displayVersion | string | 是 | 否 | 产品版本. |
incrementalVersion | string | 是 | 否 | 差异版本号. |
osReleaseType | string | 是 | 否 | The release type of the system,取值为: - Canary:Early preview releases for specific developers,不承诺API稳定性. - Beta:Publicly released to developersBeta版本,不承诺API稳定性. - Release:The official version that is publicly released to developers,承诺API稳定性. |
osFullName | string | 是 | 否 | 系统版本. |
majorVersion | number | 是 | 否 | Major版本号,Added with major version updates. |
seniorVersion | number | 是 | 否 | Senior版本号,with local architecture、Significant feature additions. |
featureVersion | number | 是 | 否 | Feature版本号,Identifies the planned new feature release. |
buildVersion | number | 是 | 否 | Build版本号,Identifies the version number of the compiled build. |
sdkApiVersion | number | 是 | 否 | 系统软件API版本. |
firstApiVersion | number | 是 | 否 | The first version of system softwareAPI版本. |
versionId | string | 是 | 否 | 版本ID. |
buildType | string | 是 | 否 | 构建类型. |
buildUser | string | 是 | 否 | 构建用户. |
buildHost | string | 是 | 否 | 构建主机. |
buildTime | string | 是 | 否 | 构建时间. |
buildRootHash | string | 是 | 否 | 构建版本Hash. |
udid7+ | string | 是 | 否 | 设备Udid. 需要权限:ohos.permission.sec.ACCESS_UDID,This permission is a system permission |
边栏推荐
- 结合“xPlus”探讨软件架构的创新与变革
- poj2935 Basic Wall Maze (2016xynu暑期集训检测 -----D题)
- The century-old Nordic luxury home appliance brand ASKO smart wine cabinet in the three-temperature area presents the Chinese Valentine’s Day, and tastes the love of the delicacy
- [Translation] Chaos Net + SkyWalking: Better observability for chaos engineering
- static linking and dynamic linking
- This notebook of concurrent programming knowledge points strongly recommended by Ali will be a breakthrough for you to get an offer from a big factory
- lvgl 实现状态提示图标自动对齐补位显示
- LeetCode 216. Combined Sum III (2022.08.04)
- STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
- Offensive World-PWN-new_easypwn
猜你喜欢
【综合类型第 35 篇】程序员的七夕浪漫时刻
012年通过修补_sss_提高扩散模型效率
In-depth understanding of timeout settings for Istio traffic management
Login function and logout function (St. Regis Takeaway)
Introduction to SD NAND Flash!
MySQL事务
教你本地编译运行一个IDEA插件,在IDEA里聊天、下棋、斗地主!
【MySQL基础】-【数据处理之增删改】
MySQL 中 auto_increment 自动插入主键值
012_SSS_ Improving Diffusion Model Efficiency Through Patching
随机推荐
poj2935 Basic Wall Maze (2016xynu暑期集训检测 -----D题)
反射修改jsessionid实现Session共享
static linking and dynamic linking
FPGA:基础入门LED灯闪烁
静态链接和动态链接
MySQL transactions
Voice-based social software development - making the most of its value
19.3 restart the Oracle environment
秘乐短视频挖矿系统开发详情
[Android]如何使用RecycleView in Kotlin project
FPGA: Basic Getting Started Button Controlling LED Lights
Still looking for a network backup resources?Hurry up to collect the following network backup resource search artifact it is worth collecting!
PCB layout must know: teach you to correctly lay out the circuit board of the op amp
LeetCode 216. Combined Sum III (2022.08.04)
力扣(LeetCode)216. 组合总和 III(2022.08.04)
记2022年七夕感慨
The fuse: OAuth 2.0 four authorized login methods must read
uniapp中的view高度设置100%
攻防世界-PWN-new_easypwn
The century-old Nordic luxury home appliance brand ASKO smart wine cabinet in the three-temperature area presents the Chinese Valentine’s Day, and tastes the love of the delicacy