import Context from "android.content.Context"; import BatteryManager from "android.os.BatteryManager"; import { UTSAndroid } from "io.dcloud.uts"; type GetBatteryInfoOptions = { success?: (res: UTSJSONObject) => void fail?: (res: UTSJSONObject) => void complete?: (res: UTSJSONObject) => void } export default function getBatteryInfo(options: GetBatteryInfoOptions) { const context = UTSAndroid.getAppContext(); if (context != null) { const manager = context.getSystemService( Context.BATTERY_SERVICE ) as BatteryManager; const level = manager.getIntProperty( BatteryManager.BATTERY_PROPERTY_CAPACITY ); const res = { errCode: 0, errSubject: "uni-getBatteryInfo", errMsg: 'getBatteryInfo:ok', level, isCharging: manager.isCharging() } options.success?.(res) options.complete?.(res) } else { const res = { errSubject: "uni-getBatteryInfo", errCode: 1001, errMsg: 'getBatteryInfo:fail getAppContext is null' } options.fail?.(res) options.complete?.(res) } }