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

边栏推荐
- PAT Grade B-B1020 Mooncake(25)
- IO流篇 -- 基于io流实现文件夹拷贝(拷贝子文件夹及子文件夹内文件)满满的干货
- ffmpeg drawtext 添加文本水印
- Assembly language (8) x86 inline assembly
- 为什么sys_class 里显示的很多表的 RELTABLESPACE 值为 0 ?
- 为什么我推荐使用智能化async?
- 2022.8.3
- 哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
- ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
- PAT乙级-B1021 个位数统计(15)
猜你喜欢
随机推荐
Excuse me if you want to write data in mysql, with flink - connector - JDBC directly is ok, but I'm in the f
Microservice Technology Stack
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
egg框架使用(一)
Weekly Report 2022-8-4
Hundred lines of code launch red hearts, why programmers lose their girlfriends!
After Keil upgrades to AC6, what changes?
ffmpeg drawtext add text watermark
ffmpeg drawtext 添加文本水印
pytorch余弦退火学习率CosineAnnealingLR的使用
Oracle临时表空间作用
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
2022.8.3
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Clothing Image Classification (Day 3)
thinkPHP5 realizes clicks (data increment/decrement)
【LeetCode】623. Add a row to the binary tree
dotnet OpenXML parsing PPT charts Getting started with area charts
放大器OPA855的噪声计算实例