鸿蒙获取电量信息【坚果派】

电量信息的使用

导入模块

import batteryInfo from '@ohos.batteryInfo';

属性

描述电池信息。

系统能力:SystemCapability.PowerManager.BatteryManager.Core

名称 类型 可读 可写 说明
batterySOC number 表示当前设备剩余电池电量百分比。
chargingStatus BatteryChargeState 表示当前设备电池的充电状态。
healthStatus BatteryHealthState 表示当前设备电池的健康状态。
pluggedType BatteryPluggedType 表示当前设备连接的充电器类型。
voltage number 表示当前设备电池的电压,单位微伏。
technology string 表示当前设备电池的技术型号。
batteryTemperature number 表示当前设备电池的温度,单位0.1摄氏度。
isBatteryPresent7+ boolean 表示当前设备是否支持电池或者电池是否在位。
batteryCapacityLevel9+ BatteryCapacityLevel 表示当前设备电池电量的等级。

完整代码

import batteryInfo from '@ohos.batteryInfo';

@Entry
@Component
struct MainPage {
    
    
  @State healthStatus: string = '健康'
  @State batterySOC: string = '剩余电池电量百分比'
  @State chargingStatus: string = '当前设备电池的充电状态'
  @State batteryTemperature: string = '电池温度'

  build() {
    
    
    Row() {
    
    
      Column() {
    
    
        Text(this.batterySOC)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(() => {
    
    

          this.batterySOC = "剩余电池电量百分比。" + batteryInfo.batterySOC.toString()


        })
        Text(this.chargingStatus)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(() => {
    
    

          this.chargingStatus = "当前设备电池的充电状态。" + batteryInfo.chargingStatus.toString()


        })
        Text(this.batteryTemperature)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(() => {
    
    

          this.batteryTemperature = "电池温度" + (batteryInfo.batteryTemperature / 10).toString()


        })
        Text(this.healthStatus)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(() => {
    
    

          this.healthStatus = "健康状况" + batteryInfo.healthStatus.toString()


        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

完毕。

猜你喜欢

转载自blog.csdn.net/qq_39132095/article/details/132777528
今日推荐