diff --git a/pages/SystemAPI/SystemAPI.vue b/pages/SystemAPI/SystemAPI.vue index 1a6948667f10ed2a717077bc14daad4f5f12fd47..04912324f505b5e9fd5c39edfa6778d4f4be4cd8 100644 --- a/pages/SystemAPI/SystemAPI.vue +++ b/pages/SystemAPI/SystemAPI.vue @@ -87,6 +87,7 @@ testGetBatteryCapacity() { getBatteryInfo({ success(res) { + console.log(res) uni.showToast({ title: "当前电量:" + res.level + '%', icon: 'none' diff --git a/uni_modules/uts-getbatteryinfo/utssdk/app-android/index.uts b/uni_modules/uts-getbatteryinfo/utssdk/app-android/index.uts index f8d5dc17fd8c3fcffd65fa72946c85de664aba35..4207108493c4be15d0dcfa3da5703dd37c7f240a 100644 --- a/uni_modules/uts-getbatteryinfo/utssdk/app-android/index.uts +++ b/uni_modules/uts-getbatteryinfo/utssdk/app-android/index.uts @@ -1,6 +1,8 @@ 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)