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

边栏推荐
- 动态库之间回调函数使用
- Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上
- 欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
- Creo 9.0 基准特征:基准点
- js 图形操作一(兼容pc、移动端实现 draggable属性 拖放效果)
- eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
- 2022.8.3
- 使用稀疏 4D 卷积对 3D LiDAR 数据中的运动对象进行后退分割(IROS 2022)
- Code Audit - PHP
- 画法几何及工程制图考试卷A卷
猜你喜欢
【Excel实战】--图表联动demo_001
sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?
How to realize the short press and long press detection of the button?
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法
Thinking and summary of the efficiency of IT R&D/development process specification
动态内存开辟(C语言)
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
15.1.1、md—md的基础语法,快速的写文本备忘录
There is only one switch, how to realize the nqa of master-slave automatic switching
随机推荐
工程制图直线投影练习
Going to book tickets tomorrow, ready to go home~~
使用 External Secrets Operator 安全管理 Kubernetes Secrets
512-color chromatogram
16 kinds of fragrant rice recipes
Dynamic memory development (C language)
ts/js function pass parameter with function writing
Walk 100 trick society
Comprehensively explain what is the essential difference between GET and POST requests?Turns out I always misunderstood
There is only one switch, how to realize the nqa of master-slave automatic switching
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
手把手教你纯c实现异常捕获try-catch组件
我的杂记链接
无题三
施一公:科学需要想象,想象来自阅读
工程制图知识点
Hundred lines of code launch red hearts, why programmers lose their girlfriends!
egg framework
Code Audit - PHP
无题十