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

边栏推荐
猜你喜欢

科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系

mysql进阶(二十七)数据库索引原理

Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g

pytorch余弦退火学习率CosineAnnealingLR的使用

CCVR eases heterogeneous federated learning based on classifier calibration

dotnet OpenXML 解析 PPT 图表 面积图入门

哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?

egg框架使用(一)

sql server中 两表查询 平均数 分组

Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上
随机推荐
How ali cloud storage database automatically to speed up the loading speed of www.cxsdkt.cn how to set up the case?
偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的
matcher中find,matches,lookingAt匹配字符串的不同之处说明
What is CRM Decision Analysis Management?
PAT乙级-B1019 数字黑洞(20)
新白娘子传奇系列
egg框架使用(一)
express hot-reload
深度学习21天——卷积神经网络(CNN):天气识别(第5天)
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
shell脚本实例
HStreamDB Newsletter 2022-07|分区模型优化、数据集成框架进一步完善
【Excel实战】--图表联动demo_001
Tanabata romantic date without overtime, RPA robot helps you get the job done
selectPage 动态改变参数方法
线程之Happens-before规则
(转)[Json]net.sf.json 和org.json 的差别及用法
阿里云存储的数据库是怎么自动加快加载速度的呢www.cxsdkt.cn怎么设置案例?
Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
Microservice Technology Stack