diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts index de7e2c1f8e575dedeec76a458d0ec86b1e0b8ee8..060043b8662663d4b6ce54b71974de7089599503 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 + + +} diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-ios/config.json b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/config.json new file mode 100644 index 0000000000000000000000000000000000000000..721b81e0240705a87ec0761aa9eb6f4e188769cb --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/config.json @@ -0,0 +1,3 @@ +{ + "deploymentTarget": "9" +} \ No newline at end of file diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts index 391654aae95e00bdd3d268bbc67163f9937c86d8..e33ab29913aa1586d6a6cbfd1bda3fa4438a8932 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts @@ -20,8 +20,8 @@ export const getBatteryInfo : GetBatteryInfo = function (options) { }; options.success?.(res); options.complete?.(res); -} - +} + export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult { // 开启电量检测 diff --git a/uni_modules/uni-getbatteryinfo/utssdk/index.d.ts b/uni_modules/uni-getbatteryinfo/utssdk/index.d.ts index bfd890c12e8182e8f96c450eb7ddce8afc2fe6c3..9f0b83c4e53a1a27957eb4b291cf3b1fc032f7d8 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/index.d.ts +++ b/uni_modules/uni-getbatteryinfo/utssdk/index.d.ts @@ -38,5 +38,5 @@ declare interface Uni { /** * 同步获取电池电量信息 */ - GetBatteryInfoSync(): UniNamespace.GetBatteryInfoSuccessCallbackResult; + getBatteryInfoSync(): UniNamespace.GetBatteryInfoSuccessCallbackResult; } diff --git a/uni_modules/uni-getbatteryinfo/utssdk/interface.uts b/uni_modules/uni-getbatteryinfo/utssdk/interface.uts index b3a60ca1dcc451559554cbcaf4eedf524e6013eb..d674221a16620c90639a24de95efbe59177ffa9d 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/interface.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/interface.uts @@ -60,12 +60,12 @@ export type GetBatteryInfoResult = { /** * 获取电量信息 * @param {GetBatteryInfoOptions} options -* +* * * @tutorial https://uniapp.dcloud.net.cn/api/system/batteryInfo.html * @platforms APP-IOS = ^9.0,APP-ANDROID = ^22 * @since 3.6.11 -* +* * @assert () => success({errCode: 0, errSubject: "uni-getBatteryInfo", errMsg: "getBatteryInfo:ok", level: 60, isCharging: false }) * @assert () => fail({errCode: 1001, errSubject: "uni-getBatteryInfo", errMsg: "getBatteryInfo:fail getAppContext is null" }) */ @@ -79,7 +79,7 @@ interface Uni { /** * 获取电池电量信息 * @description 获取电池电量信息 - * @param {GetBatteryInfoOptions} options + * @param {GetBatteryInfoOptions} options * @example * ```typescript * uni.getBatteryInfo({ @@ -107,7 +107,7 @@ interface Uni { * @uniVueVersion 2,3 //支持的vue版本 * */ - getBatteryInfo(options : GetBatteryInfoOptions) : void, + getBatteryInfo (options : GetBatteryInfoOptions) : void, /** * 同步获取电池电量信息 * @description 获取电池电量信息 @@ -134,6 +134,6 @@ interface Uni { * @uniVueVersion 2,3 //支持的vue版本 * */ - getBatteryInfoSync() : GetBatteryInfoResult + getBatteryInfoSync():GetBatteryInfoResult -} \ No newline at end of file +}