diff --git a/pages/index/index.vue b/pages/index/index.vue index f64e148578503d6c5cbc4b004ab9acff01ac86a3..87b239c900741462ccd85877896df7d099eff9ac 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -16,6 +16,7 @@ + @@ -219,6 +220,12 @@ } }) }, + + testGetBatteryInfoSync() { + let ret = uni.getBatteryInfoSync() + console.log(ret) + }, + testSetUserCaptureScreen() { let flag = this.setUserCaptureScreenFlag; uni.setUserCaptureScreen({ diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts index 319368c02692c8ac451d996ee2f64a31aa8c0048..d7fe8aca5235bf8fc0a22638bc8c9fbd4d15ef59 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts @@ -2,8 +2,11 @@ import Context from "android.content.Context"; import BatteryManager from "android.os.BatteryManager"; import { UTSAndroid } from "io.dcloud.uts"; -import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoFail, GetBatteryInfoOptions } from '../interface.uts' - +import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoFail, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts' +/** + * 异步获取电量 + * @param {Object} options + */ export const getBatteryInfo : GetBatteryInfo = function (options) { const context = UTSAndroid.getAppContext(); if (context != null) { @@ -30,4 +33,37 @@ export const getBatteryInfo : GetBatteryInfo = function (options) { options.fail?.(res) options.complete?.(res) } +} + +/** + * 同步获取电量示例 + */ +export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult { + + const context = UTSAndroid.getAppContext(); + 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; + } + + } \ No newline at end of file