From 1a3a7272e5bf622ae4e48d9aea511d318ac2d979 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Mon, 29 May 2023 18:18:25 +0800 Subject: [PATCH] =?UTF-8?q?android=20=E8=A1=A5=E5=85=85=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=B5=E9=87=8F=E7=9A=84=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages.json | 7 ---- pages/index/index.vue | 7 ++++ .../utssdk/app-android/index.uts | 40 ++++++++++++++++++- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/pages.json b/pages.json index 94e865b..24f82b8 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 e62e0ac..8a23d5c 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 319368c..d7fe8ac 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 -- GitLab