当前位置:网站首页>开源一夏|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

边栏推荐
- Creo 9.0 基准特征:基准坐标系
- Is there a problem with writing this?How to synchronize data in sql-client
- 【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
- The Secrets of the Six-Year Team Leader | The Eight Most Important Soft Skills of Programmers
- 动态库之间回调函数使用
- 随时牵手 不要随意分手[转帖]
- 2022-08-01 Review the basic binary tree and operations
- 2022.8.3
- openpyxl to manipulate Excel files
- MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
猜你喜欢
工程制图知识点
“充钱”也难治快手的“亏亏亏”?
Dry goods!Generative Model Evaluation and Diagnosis
让程序员崩溃的N个瞬间(非程序员误入)
Seata source code analysis: initialization process of TM RM client
使用HBuilder离线本地打包ipa教程
DTcloud 装饰器
画法几何及工程制图考试卷A卷
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
Creo 9.0 基准特征:基准平面
随机推荐
Does flink cdc support synchronization from oracle dg library?
无题十三
HStreamDB Newsletter 2022-07|分区模型优化、数据集成框架进一步完善
Walk 100 trick society
IT研发/开发流程规范效能的思考总结
512-color chromatogram
express hot-reload
Concurrent CAS
Creo 9.0 基准特征:基准坐标系
Overall design and implementation of Kubernetes-based microservice project
使用 External Secrets Operator 安全管理 Kubernetes Secrets
工程制图知识点
无题三
tensorflow.keras cannot introduce layers
【ASM】字节码操作 方法的初始化 Frame
How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
【LeetCode】623. 在二叉树中增加一行
2022.8.3
C语言-数组
微信小程序请求封装