当前位置:网站首页>Open Source Summer | How OpenHarmony Query Device Type (eTS)
Open Source Summer | How OpenHarmony Query Device Type (eTS)
2022-08-05 09:32:00 【InfoQ】
- 通过jsInterface query to specify system parameters(const.build.characteristics)to determine the device type,详见系统属性.
// @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)
//通过jsInterface query to specify system parameters(const.build.characteristics)to determine the device type
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);
}
})
//通过displayQuery the properties of the display device(Including screen width、high and screen density, etc.)
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%')
}
}
How to query the screen/窗口尺寸
- 通过displayQuery the properties of the display device(Including screen width、high and screen density, etc.),详见屏幕属性.
// @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)
//通过jsInterface query to specify system parameters(const.build.characteristics)to determine the device type
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;
})
//通过displayQuery the properties of the display device(Including screen width、high and screen density, etc.)
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

边栏推荐
- 基于 Kubernetes 的微服务项目整体设计与实现
- Excuse me, guys, is it impossible to synchronize two databases in real time using Flink SQL CDC?
- js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
- Oracle临时表空间作用
- 汇编语言(8)x86内联汇编
- There is only one switch, how to realize the nqa of master-slave automatic switching
- C语言-数组
- 周报2022-8-4
- 无题十二
- Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
猜你喜欢

CCVR基于分类器校准缓解异构联邦学习

Assembly language (8) x86 inline assembly

Creo 9.0 基准特征:基准坐标系

如何实现按键的短按、长按检测?

How to realize the short press and long press detection of the button?

CPU的亲缘性affinity

Creo 9.0 基准特征:基准平面

There is only one switch, how to realize the nqa of master-slave automatic switching

eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑

Redis源码解析:Redis Cluster
随机推荐
PAT乙级-B1021 个位数统计(15)
汇编语言(8)x86内联汇编
15.1.1、md—md的基础语法,快速的写文本备忘录
Redis源码解析:Redis Cluster
Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
C语言-数组
正则表达式replaceFirst()方法具有什么功能呢?
为什么我推荐使用智能化async?
Keil升级到AC6后,到底有哪些变化?
Happens-before rules for threads
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(下)
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)
无题四
无题十一
Xcode 12 ld: symbol(s) not found for architecture armv64
无题十
19.服务器端会话技术Session
PAT Level B - B1021 Single Digit Statistics (15)
无题十二
PAT Class B-B1019 Digital Black Hole (20)