当前位置:网站首页>开源一夏|OpenHarmony如何查询设备类型(eTS)
开源一夏|OpenHarmony如何查询设备类型(eTS)
2022-08-05 09:14:00 【InfoQ】
- 通过js接口查询指定系统参数(const.build.characteristics)进而确定设备类型,详见系统属性。
// @ts-nocheck
import parameter from '@ohos.systemParameter'
@Entry
@Component
struct 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%')
}
}
// @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
@Component
struct 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
@Component
struct 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
边栏推荐
- 歌词整理
- The Seven Weapons of Programmers
- MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
- 使用HBuilder离线本地打包ipa教程
- How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
- tensorflow.keras cannot introduce layers
- 全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
- 嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
- Weekly Report 2022-8-4
- thinkPHP5 realizes clicks (data increment/decrement)
猜你喜欢
ECCV 2022 Oral 视频实例分割新SOTA:SeqFormer&IDOL及CVPR 2022 视频实例分割竞赛冠军方案...
并发之CAS
How to realize the short press and long press detection of the button?
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)
Concurrent CAS
Embedded practice ---- based on RT1170 transplant memtester to do SDRAM test (25)
Weekly Report 2022-8-4
2022.8.3
干货!生成模型的评价与诊断
工程制图直线投影练习
随机推荐
Hundred lines of code launch red hearts, why programmers lose their girlfriends!
深度学习21天——卷积神经网络(CNN):天气识别(第5天)
无题九
openpyxl操作Excel文件
Dynamic memory development (C language)
上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》
好资料汇总
Concurrent CAS
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
Xcode 12 ld: symbol(s) not found for architecture armv64
CVPR 2022 | 将X光图片用于垃圾分割,港中大(深圳)探索大规模智能垃圾分类
周报2022-8-4
Going to book tickets tomorrow, ready to go home~~
2022/8/4 考试总结
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
CCVR基于分类器校准缓解异构联邦学习
【Excel实战】--图表联动demo_001
The difference between beautiful MM and ordinary MM
【LeetCode】623. 在二叉树中增加一行