From c06c1eb6dd67908d112c72e8bb769b40cb26be53 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Thu, 21 Sep 2023 11:49:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=85=85=E7=94=B5=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utssdk/app-android/index.uts | 71 +++++++++++-------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts index de7e2c1f..060043b8 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts @@ -2,12 +2,16 @@ import Context from "android.content.Context"; import BatteryManager from "android.os.BatteryManager"; import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts' +import IntentFilter from 'android.content.IntentFilter'; +import Intent from 'android.content.Intent'; /** * 异步获取电量 */ -export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) { +export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) { + + const context = UTSAndroid.getAppContext(); if (context != null) { const manager = context.getSystemService( @@ -15,21 +19,21 @@ export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInf ) as BatteryManager; 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 : GetBatteryInfoSuccess = { errMsg: 'getBatteryInfo:ok', level, - isCharging: manager.isCharging() + isCharging: isCharging } options.success?.(res) options.complete?.(res) } else { - // const res : GetBatteryInfoFail = { - // errSubject: "uni-getBatteryInfo", - // errCode: 1001, - // errMsg: 'getBatteryInfo:fail getAppContext is null', - // cause: null - // } const res = new UniError("uni-getBatteryInfo", 1001, "getBatteryInfo:fail getAppContext is null") options.fail?.(res) options.complete?.(res) @@ -40,31 +44,40 @@ export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInf * 同步获取电量示例 */ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult { - + const context = UTSAndroid.getAppContext(); - if (context != null) { + if (context != null) { + + const manager = context.getSystemService( Context.BATTERY_SERVICE ) as BatteryManager; const level = manager.getIntProperty( BatteryManager.BATTERY_PROPERTY_CAPACITY ); - - const res : GetBatteryInfoResult = { - level: level, - isCharging: manager.isCharging() - }; - return res; - } else { - /** - * 无有效上下文 - */ - const res : GetBatteryInfoResult = { - level: -1, - isCharging: false - }; - return res; + + 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 : GetBatteryInfoResult = { + level: level, + isCharging: isCharging + }; + + return res; + } + else { + /** + * 无有效上下文 + */ + const res : GetBatteryInfoResult = { + level: -1, + isCharging: false + }; + return res; } - - -} \ No newline at end of file + + +} -- GitLab