提交 ef9513ad 编写于 作者: 杜庆泉's avatar 杜庆泉

优化了充电状态获取

上级 33a52411
......@@ -87,6 +87,7 @@
testGetBatteryCapacity() {
getBatteryInfo({
success(res) {
console.log(res)
uni.showToast({
title: "当前电量:" + res.level + '%',
icon: 'none'
......
import Context from "android.content.Context";
import BatteryManager from "android.os.BatteryManager";
import { UTSAndroid } from "io.dcloud.uts";
import IntentFilter from 'android.content.IntentFilter';
import Intent from 'android.content.Intent';
export type GetBatteryInfoOptions = {
......@@ -19,12 +21,19 @@ export function getBatteryInfo(options: GetBatteryInfoOptions) {
const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const res = {
errCode: 0,
errSubject: "uni-getBatteryInfo",
errMsg: "getBatteryInfo:ok",
level,
isCharging: manager.isCharging()
isCharging: isCharging
}
options.success?.(res)
options.complete?.(res)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册