diff --git a/pages.json b/pages.json index 94e865b2dc70c21b8ec31e474d1a850bb19af68e..24f82b827fe2c372169caf32fe7b9b2b28fa4037 100644 --- a/pages.json +++ b/pages.json @@ -5,13 +5,6 @@ "style": { "navigationBarTitleText": "uni-app" } - }, - { - "path": "pages/autotest/uni-getbatteryinfo/uni-getbatteryinfo", - "style": { - "navigationBarTitleText": "uni-getbatteryinfo", - "enablePullDownRefresh": false - } } ], "globalStyle": { diff --git a/pages/index/index.vue b/pages/index/index.vue index e62e0accda636f636264eb779c7cf90cf7a67f7f..8a23d5c0b577123c1e012980c22aaa7abfcd9596 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -16,6 +16,7 @@ + @@ -222,6 +223,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