Skip to main content
版本:v3

Battery

API_LEVEL 2.0 开始支持,API 兼容性请参考 API_LEVEL

电量传感器。

方法

getCurrent

获取当前设备电量百分比,范围 0 - 100

getCurrent(): number

onChange

注册电量变化事件监听回调函数

onChange(callback: () => void): void

offChange

取消电量变化事件监听回调函数

offChange(callback: () => void): void

代码示例

import { Battery } from '@zos/sensor'

const battery = new Battery()
const current = battery.getCurrent()

const callback = () => {
console.log(battery.getCurrent())
}

battery.onChange(callback)

// When not needed for use
battery.offChange(callback)