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

优化了充电状态获取

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