diff --git a/uni_modules/uni-getbatteryinfo/changelog.md b/uni_modules/uni-getbatteryinfo/changelog.md index f17dd2de1cee9bcbaddcc6a219af2bbf5055a9dd..03f2110d87b74dec98a92f48f4e18b227cff4fad 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 a978085f0578f4cd28de9f2b593945bffeb5515d..98192f2f0946b860374dd18fc5c10f94d1a882e1 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 4fdced2ee3aa9d80eafdffdc5f06d522c7a1f08a..473f1a07d3f5b227f9bb1caa1099ddfd99d254da 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 5944a774bdc27de75cbe441c1af274dfef4ab8ac..1ee373a514964b33fd47e1bd59170fb22661c0d9 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 06287bc468d46e1c12f79e455a934fcbea7c52c5..6eabfcff9737dcd28de56bdb67dbb9c96287dcb8 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 5124b3de21a08a351d2d7eb631aaf96a600aa147..3bdfa9184b26d8df7a53aa9eb61c1ef37d66a8a9 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) +}