From 2722c73599a59662643847ebd45a4cb1ae9d37aa Mon Sep 17 00:00:00 2001 From: yanyilin Date: Fri, 15 Sep 2023 20:12:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0uni-getbatteryinfo=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=88=B01.3.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uni-getbatteryinfo/changelog.md | 7 +- .../utssdk/app-android/index.uts | 136 +++++++++--------- .../utssdk/mp-alipay/index.js | 3 + .../utssdk/mp-baidu/index.js | 3 + .../uni-getbatteryinfo/utssdk/mp-qq/index.js | 3 + .../utssdk/mp-weixin/index.js | 3 + 6 files changed, 86 insertions(+), 69 deletions(-) diff --git a/uni_modules/uni-getbatteryinfo/changelog.md b/uni_modules/uni-getbatteryinfo/changelog.md index f17dd2de..03f2110d 100644 --- a/uni_modules/uni-getbatteryinfo/changelog.md +++ b/uni_modules/uni-getbatteryinfo/changelog.md @@ -1,7 +1,12 @@ +## 1.3.1(2023-09-15) +app端适配使用UniError + ## 1.3.0(2023-05-30) -+ 新增 同步获取电量api +新增 同步获取电量api + ## 1.2.0(2022-10-17) 实现百度小程序/支付宝小程序/QQ小程序获取电量 + ## 1.1.0(2022-10-17) 实现ios平台获取电量 diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts index a978085f..98192f2f 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts @@ -1,69 +1,69 @@ -import Context from "android.content.Context"; -import BatteryManager from "android.os.BatteryManager"; -import { UTSAndroid } from "io.dcloud.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) { - const manager = context.getSystemService( - Context.BATTERY_SERVICE - ) as BatteryManager; - const level = manager.getIntProperty( - BatteryManager.BATTERY_PROPERTY_CAPACITY - ); - const res : GetBatteryInfoSuccess = { - errMsg: 'getBatteryInfo:ok', - level, - isCharging: manager.isCharging() - } - options.success?.(res) - options.complete?.(res) - } else { - const res : GetBatteryInfoFail = { - errSubject: "uni-getBatteryInfo", - errCode: 1001, - errMsg: 'getBatteryInfo:fail getAppContext is null', - cause: null - } - 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; - } - - +import Context from "android.content.Context"; +import BatteryManager from "android.os.BatteryManager"; +import { UTSAndroid } from "io.dcloud.uts"; + +import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts' +/** + * 异步获取电量 + */ +export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) { + 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 : GetBatteryInfoSuccess = { + errMsg: 'getBatteryInfo:ok', + level, + isCharging: manager.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) + } +} + +/** + * 同步获取电量示例 + */ +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 diff --git a/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js b/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js index 4fdced2e..473f1a07 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js @@ -1,3 +1,6 @@ export function getBatteryInfo(options) { return my.getBatteryInfo(options) } +export function getBatteryInfoSync(options) { + return my.getBatteryInfoSync(options) +} diff --git a/uni_modules/uni-getbatteryinfo/utssdk/mp-baidu/index.js b/uni_modules/uni-getbatteryinfo/utssdk/mp-baidu/index.js index 5944a774..1ee373a5 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/mp-baidu/index.js +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-baidu/index.js @@ -1,3 +1,6 @@ export function getBatteryInfo(options) { return swan.getBatteryInfo(options) } +export function getBatteryInfoSync(options) { + return swan.getBatteryInfoSync(options) +} diff --git a/uni_modules/uni-getbatteryinfo/utssdk/mp-qq/index.js b/uni_modules/uni-getbatteryinfo/utssdk/mp-qq/index.js index 06287bc4..6eabfcff 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/mp-qq/index.js +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-qq/index.js @@ -1,3 +1,6 @@ export function getBatteryInfo(options) { return qq.getBatteryInfo(options) } +export function getBatteryInfoSync(options) { + return qq.getBatteryInfoSync(options) +} diff --git a/uni_modules/uni-getbatteryinfo/utssdk/mp-weixin/index.js b/uni_modules/uni-getbatteryinfo/utssdk/mp-weixin/index.js index 5124b3de..3bdfa918 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/mp-weixin/index.js +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-weixin/index.js @@ -1,3 +1,6 @@ export function getBatteryInfo(options) { return wx.getBatteryInfo(options) } +export function getBatteryInfoSync(options) { + return wx.getBatteryInfoSync(options) +} -- GitLab