提交 2722c735 编写于 作者: DCloud-yyl's avatar DCloud-yyl

更新uni-getbatteryinfo模块到1.3.1

上级 1b3ad13e
## 1.3.1(2023-09-15)
app端适配使用UniError
## 1.3.0(2023-05-30) ## 1.3.0(2023-05-30)
+ 新增 同步获取电量api 新增 同步获取电量api
## 1.2.0(2022-10-17) ## 1.2.0(2022-10-17)
实现百度小程序/支付宝小程序/QQ小程序获取电量 实现百度小程序/支付宝小程序/QQ小程序获取电量
## 1.1.0(2022-10-17) ## 1.1.0(2022-10-17)
实现ios平台获取电量 实现ios平台获取电量
......
import Context from "android.content.Context"; import Context from "android.content.Context";
import BatteryManager from "android.os.BatteryManager"; import BatteryManager from "android.os.BatteryManager";
import { UTSAndroid } from "io.dcloud.uts"; import { UTSAndroid } from "io.dcloud.uts";
import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoFail, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts' import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts'
/** /**
* 异步获取电量 * 异步获取电量
* @param {Object} options */
*/ export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) {
export const getBatteryInfo : GetBatteryInfo = function (options) { const context = UTSAndroid.getAppContext();
const context = UTSAndroid.getAppContext(); if (context != null) {
if (context != null) { const manager = context.getSystemService(
const manager = context.getSystemService( Context.BATTERY_SERVICE
Context.BATTERY_SERVICE ) as BatteryManager;
) as BatteryManager; const level = manager.getIntProperty(
const level = manager.getIntProperty( BatteryManager.BATTERY_PROPERTY_CAPACITY
BatteryManager.BATTERY_PROPERTY_CAPACITY );
); const res : GetBatteryInfoSuccess = {
const res : GetBatteryInfoSuccess = { errMsg: 'getBatteryInfo:ok',
errMsg: 'getBatteryInfo:ok', level,
level, isCharging: manager.isCharging()
isCharging: manager.isCharging() }
} options.success?.(res)
options.success?.(res) options.complete?.(res)
options.complete?.(res) } else {
} else { // const res : GetBatteryInfoFail = {
const res : GetBatteryInfoFail = { // errSubject: "uni-getBatteryInfo",
errSubject: "uni-getBatteryInfo", // errCode: 1001,
errCode: 1001, // errMsg: 'getBatteryInfo:fail getAppContext is null',
errMsg: 'getBatteryInfo:fail getAppContext is null', // cause: null
cause: null // }
} const res = new UniError("uni-getBatteryInfo", 1001, "getBatteryInfo:fail getAppContext is null")
options.fail?.(res) options.fail?.(res)
options.complete?.(res) options.complete?.(res)
} }
} }
/** /**
* 同步获取电量示例 * 同步获取电量示例
*/ */
export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult { export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult {
const context = UTSAndroid.getAppContext(); const context = UTSAndroid.getAppContext();
if (context != null) { if (context != null) {
const manager = context.getSystemService( const manager = context.getSystemService(
Context.BATTERY_SERVICE Context.BATTERY_SERVICE
) as BatteryManager; ) as BatteryManager;
const level = manager.getIntProperty( const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY BatteryManager.BATTERY_PROPERTY_CAPACITY
); );
const res : GetBatteryInfoResult = { const res : GetBatteryInfoResult = {
level: level, level: level,
isCharging: manager.isCharging() isCharging: manager.isCharging()
}; };
return res; return res;
} else { } else {
/** /**
* 无有效上下文 * 无有效上下文
*/ */
const res : GetBatteryInfoResult = { const res : GetBatteryInfoResult = {
level: -1, level: -1,
isCharging: false isCharging: false
}; };
return res; return res;
} }
} }
\ No newline at end of file
export function getBatteryInfo(options) { export function getBatteryInfo(options) {
return my.getBatteryInfo(options) return my.getBatteryInfo(options)
} }
export function getBatteryInfoSync(options) {
return my.getBatteryInfoSync(options)
}
export function getBatteryInfo(options) { export function getBatteryInfo(options) {
return swan.getBatteryInfo(options) return swan.getBatteryInfo(options)
} }
export function getBatteryInfoSync(options) {
return swan.getBatteryInfoSync(options)
}
export function getBatteryInfo(options) { export function getBatteryInfo(options) {
return qq.getBatteryInfo(options) return qq.getBatteryInfo(options)
} }
export function getBatteryInfoSync(options) {
return qq.getBatteryInfoSync(options)
}
export function getBatteryInfo(options) { export function getBatteryInfo(options) {
return wx.getBatteryInfo(options) return wx.getBatteryInfo(options)
} }
export function getBatteryInfoSync(options) {
return wx.getBatteryInfoSync(options)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册