当前位置:网站首页>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
边栏推荐
猜你喜欢
2.4G无线收发模块的应用
干货!生成模型的评价与诊断
Custom filters and interceptors implement ThreadLocal thread closure
CCVR eases heterogeneous federated learning based on classifier calibration
sql server中 两表查询 平均数 分组
CCVR基于分类器校准缓解异构联邦学习
Oracle temporary table space role
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
IDEA执行Test操作导致数据插入时出现了重复数据
营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
随机推荐
Hundred lines of code launch red hearts, why programmers lose their girlfriends!
Going to book tickets tomorrow, ready to go home~~
偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的
js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
The Seven Weapons of Programmers
无题八
Xcode 12 ld: symbol(s) not found for architecture armv64
歌词整理
PAT Level B - B1021 Single Digit Statistics (15)
How to realize the short press and long press detection of the button?
Custom filters and interceptors implement ThreadLocal thread closure
Assembly language (8) x86 inline assembly
自定义过滤器和拦截器实现ThreadLocal线程封闭
无题九
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
15.1.1、md—md的基础语法,快速的写文本备忘录
只有一台交换机,如何实现主从自动切换之nqa
C语言的高级用法
新白娘子传奇系列
Creo 9.0 基准特征:基准点