Day01-HarmonyOS“守护助手”项目学习_获取软硬件信息的API
·
- 项目克隆:
git clone -b template https://gitee.com/Megasu/hm_guardian.git
1.获取设备信息
1.1 获取手机名称
- 官方文档:@ohos.deviceInfo (设备信息)
marketName:外部产品系列(示例:HUAWEI Mate 60 Pro)- 编写调用函数
//获取设备产品名称 getDeviceInfo(){ //模拟器emulator this.deviceName = deviceInfo.marketName // 传递给全局变量 }
1.2 通过Debug获取信息
- 官方文档@ohos.hidebug (Debug调试)
- 编写调用函数
//通过Debug获取信息 getDebugInfo(){ this.cpuUsage = hidebug.getCpuUsage() // 获取CPU占用率 this.memorySize = Number(hidebug.getPss()) / 1000 //获取内存占用 }
1.2.1 获取CPU使用率
hidebug.getCpuUsage:获取进程的CPU使用率- 如占用率为50%,则返回0.5
1.2.2 获取内存占用大小
hidebug.getPss:获取进程占用的物理内存大小- 返回值:
bigint(大整数类型) - 返回单位:
KB
- 返回值:
1.3 获取存储空间剩余大小
- 官方文档@ohos.file.statvfs (文件系统空间统计)
statfs.getFreeSizeSync:获取剩余内存大小- 参数:
path文件路径- 通过
getContext()获取系统上下文 context.filesDir:系统文件路径
- 通过
- 参数:
- 编写调用函数
//获取剩余内存大小 getStatfsInfo(){ const context = getContext() // 获取文件系统空闲大小 Byte this.memorySize = statfs.getFreeSizeSync(context.filesDir) / (1000 * 1000 * 1000) }
1.4获取电量信息
- 官方文档@ohos.batteryInfo (电量信息)
batterySOC:表示当前设备剩余电池电量百分比- 编写调用函数
//获取剩余电量 getBatterInfo(){ this.batteryState = batteryInfo.batterySOC }
2.动态刷新页面
2.1 切换页面时,刷新页面
- 解决方案:通过获取
Tabs组件的index,当index发生时重新获取页面数据
- 侦听
Tabs组件的index// @Provide 装饰器和 @Consume 装饰器:与后代组件双向同步 @Consume @Watch('onTabsChange') currentIndex: number // @Watch 当被@State、@Prop、@Link、@Provide、@Consume修饰的变量值发生改变时,执行回调方法 - 编写回调方法
onTabsChange() {
if (this.currentIndex === 2) {
this.getDebugInfo()
this.getStatfsInfo()
this.getBatterInfo()
}
}
// 当切换到"我的"页面时,重新调用信息
2.2 隐藏页面时,刷新页面
- 解决方案:通过侦听页面的状态,当页面被移至后台运行时刷新页面
- 侦听页面状态
//因为"我的"页面时在index.ets中Tabs的页面,因此在index.ets中记录页面状态 @Provide isPageShow: boolean = false // 使用@Provide传递给子组件数据 onPageShow() { this.isPageShow = true themeManager.enableFullScreen() } onPageHide() { this.isPageShow = false themeManager.disableFullScreen() } - 接收页面状态
@Consume isPageShow: boolean - 修改页面刷新函数的逻辑
onTabsChange() { // AlertDialog.show({ message: this.currentIndex.toString() }) if (this.currentIndex === 2 && this.isPageShow === true) { this.getDebugInfo() this.getStatfsInfo() this.getBatterInfo() } }
3. 获取设备硬件信息
3.1 获取设备参数信息
3.1.1 获取设备名称
marketName:设备名称
this.marketName = deviceInfo.marketName // 设备名称
3.1.2 获取设备系统版本
osFullName:系统版本
this.osFullName = deviceInfo.osFullName // 系统版本
3.1.3 获取系统软件API版本
sdkApiVersion:获取API版本
this.sdkApiVersion = deviceInfo.sdkApiVersion.toString() //API版本
3.1.4 获取设备类型
deviceType:获取设备类型
this.deviceType = deviceInfo.deviceType // 获取设备类型
3.2 获取进程信息
3.2.1 获取系统运行时间
uptime:获取设备运行时间- 返回值单位:秒
this.uptime = process.uptime() // 系统运行时间
3.2.1.1 获取系统上次运行时间
- 导包,调用
dayjs
import dayjs from 'dayjs' // 导入dayjs
import duration from 'dayjs/plugin/duration' // 导入时间处理模块
- 修改函数参数
// ListRow({ title: '上次启动', value: String(this.uptime) })
ListRow({
title: '上次启动',
value: dayjs()
.subtract(this.uptime, 'second')
.format('YYYY-MM-DD HH:mm:ss')
})
3.2.1.2 获取系统开机时间
- 导包,调用
dayjs
import dayjs from 'dayjs' // 导入dayjs
import duration from 'dayjs/plugin/duration' // 导入时间处理模块
dayjs.extend(duration) // 注册 duration 运行时长插件
- 修改函数参数
// ListRow({ title: '运行时间', value: String(this.uptime) })
ListRow({
title: '运行时间',
value: dayjs.duration(this.uptime, 'second')
.format('D天H小时mm分')
})
4.获取设备网络信息
4.1 权限声明-运行应用获取网络信息
hasDefaultNetSync:获取网络是否连接- 返回值类型:布尔型
- 使用前,必须添加权限
- 进入
module.json5加入requestPermissions,配置权限
//权限
"requestPermissions": [
{
"name": "ohos.permission.GET_NETWORK_INFO" //查看网络信息权限
}
],
- 调用
hasDefaultNetSync方法,并且进行判断
//获取网络信息
getConnectionInfo() {
const isHasDefaultNot = connection.hasDefaultNetSync() // 查看是否有网络
// AlertDialog.show({ message: isHasDefaultNot + '' }) // 弹窗查看
if (isHasDefaultNot) {
this.netBearType = '有网络'
} else {
this.netBearType = '无网络'
}
}
4.2 判断网络类型
getNetCapabilities:获取网络能力集- 参数:
NetHandle,设备中已激活的数据网络getDefaultNetSync:获取默认激活的数据网络
- 返回值:
netHandle,网络能力集 - 必须在有网络的情况下才能获取
- 参数:
- 获取网络类型需要按照以下步骤
- 获取设备中已激活的数据网络
getDefaultNetSync - 获取网络能力集
getNetCapabilities - 获取
bearerTypes,进行比对
//获取网络类型 getNetBearType() { const netHandle = connection.getDefaultNetSync() // 获取已激活的数据网络 const netCapabilities = connection.getNetCapabilitiesSync(netHandle) // 获取网络能力集 if (netCapabilities.bearerTypes.includes(connection.NetBearType.BEARER_ETHERNET)) { this.netBearType = "以太网网络" // 网线 } else if (netCapabilities.bearerTypes.includes(connection.NetBearType.BEARER_WIFI)) { this.netBearType = "Wi-Fi网络" } else if (netCapabilities.bearerTypes.includes(connection.NetBearType.BEARER_CELLULAR)) { this.netBearType = '蜂窝网络' } else { this.netBearType = '有网络' } }.includes(目标)方法表示判断数组内是否有目标元素connection.NetBearType.*:表示鸿蒙提供的几种网络类型netCapabilities.bearerTypes:是网络能力集中表示网络连接类型的数组bearerTypes,其中当前设备已连接的网络类型代码0:BEARER_CELLULAR,蜂窝网络1:BEARER_WIFI,Wi-Fi网络2:BEARER_BLUETOOTH,蓝牙网络3:BEARER_ETHERNET,以太网网络4:BEARER_VPN,VPN网络
- 官方文档@ohos.net.connection (网络连接管理)-NetBearType
- 获取设备中已激活的数据网络
4.3 获取设备当前网络IP地址
getConnectionPropertiesSync:获取网络连接信息- 参数:
NetHandle,设备中已激活的数据网络getDefaultNetSync:获取默认激活的数据网络
- 返回值:
ConnectionProperties,网络连接信息 - 必须在有网络的情况下才能获取
- 参数:
- 获取设备当前网络IP地址需要按照以下步骤
- 获取设备中已激活的数据网络
getDefaultNetSync - 获取网络连接信息
getConnectionPropertiesSync - 获取链路信息
ConnectionProperties.linkAddresses[0] - 获取链路信息中的IP地址
linkAddress.address.address
//获取网络连接信息IP地址 getProperties() { //获取默认网络 const netHandle = connection.getDefaultNetSync() //获取网络连接信息 const ConnectionProperties = connection.getConnectionPropertiesSync(netHandle) //提取链路信息 const linkAddress = ConnectionProperties.linkAddresses[0] if (linkAddress) { // 提取链路信息中的IP地址 this.IPAddress = linkAddress.address.address } } - 获取设备中已激活的数据网络
5.获取设备屏幕相关信息
- 官方文档@ohos.display (屏幕属性)
getDefaultDisplaySync:获取默认手机屏幕的信息- 返回值:
display对象width:宽height:高refreshRate:刷新率densityDPI:像素密度
//获取屏幕属性 getDisplayInfo() { const defaultDisplay = display.getDefaultDisplaySync() this.displayHeight = defaultDisplay.height //高 this.displayWidth = defaultDisplay.width //款 this.displayDensityDPI = defaultDisplay.densityDPI //像素密度DPI this.displayRefreshRate = defaultDisplay.refreshRate //刷新率 }- 返回值:
6.获取设备传感器信息
- 官方文档@ohos.sensor (传感器)
getSensorListSync:获取所有传感器信息sensorName:传感器名称vendorName:传感器供应商firmwareVersion:传感器固件版本hardwareVersion:传感器硬件版本sensorId:传感器类型idmaxRange:传感器测量范围的最大值minSamplePeriod:允许的最小采样周期maxSamplePeriod:允许的最大采样周期precision:传感器精度power:传感器功率的估计值,单位:mAsensorIndex:传感器索引deviceId:设备IDdeviceName:设备名称isLocalSensor:是否本地传感器- 所有传感器
export interface SensorName { sensorId: number sensorName: string } // 传感器名称 export const SENSOR_NAME_LIST: SensorName[] = [ { "sensorId": 1, "sensorName": "加速度传感器" }, { "sensorId": 2, "sensorName": "陀螺仪传感器" }, { "sensorId": 5, "sensorName": "环境光传感器" }, { "sensorId": 6, "sensorName": "磁场传感器" }, { "sensorId": 8, "sensorName": "气压计传感器" }, { "sensorId": 10, "sensorName": "霍尔传感器" }, { "sensorId": 12, "sensorName": "接近光传感器" }, { "sensorId": 13, "sensorName": "湿度传感器" }, { "sensorId": 256, "sensorName": "方向传感器" }, { "sensorId": 257, "sensorName": "重力传感器" }, { "sensorId": 258, "sensorName": "线性加速度传感器" }, { "sensorId": 259, "sensorName": "旋转矢量传感器" }, { "sensorId": 260, "sensorName": "环境温度传感器" }, { "sensorId": 261, "sensorName": "未校准磁场传感器" }, { "sensorId": 263, "sensorName": "未校准陀螺仪传感器" }, { "sensorId": 264, "sensorName": "有效运动传感器" }, { "sensorId": 265, "sensorName": "计步检测传感器" }, { "sensorId": 266, "sensorName": "计步传感器" }, { "sensorId": 278, "sensorName": "心率传感器" }, { "sensorId": 280, "sensorName": "佩戴检测传感器" }, { "sensorId": 281, "sensorName": "未校准加速度计传感器" } ]- 判断传感器是否存在
//1.获取所有的传感器ID //获取所有传感器的信息 getSensorInfo() { const sensorList = sensor.getSensorListSync() // 获取所有传感器信息 // 由于信息过多,我们可以对信息进行映射 // 对传感器信息进行map映射 this.supportSensorIds = sensorList.map(item => item.sensorId) //获取所有的传感器ID // AlertDialog.show({ message: JSON.stringify(this.supportSensorIds, null, 2) }) //测试 } //2.通过循环,将存在的传感器改为"有" ForEach(SENSOR_NAME_LIST, (item: SensorName) => { ListRow({ title: item.sensorName, value: this.supportSensorIds.includes(item.sensorId) ? '有' : '-' }) })
更多推荐


所有评论(0)