提交 cfdc2863 编写于 作者: DCloud-yyl's avatar DCloud-yyl 提交者: DCloud-WZF

同步uni-getbatteryinfo插件代码

上级 64bb5e6d
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 { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts' import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts'
import IntentFilter from 'android.content.IntentFilter'; import IntentFilter from 'android.content.IntentFilter';
import Intent from 'android.content.Intent'; import Intent from 'android.content.Intent';
import { GetBatteryInfoFailImpl } from '../unierror';
/** /**
* 异步获取电量 * 异步获取电量
*/ */
export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) { export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) {
const context = UTSAndroid.getAppContext(); const context = UTSAndroid.getAppContext();
if (context != null) { if (context != null) {
const manager = context.getSystemService( const manager = context.getSystemService(
...@@ -34,21 +33,18 @@ export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInf ...@@ -34,21 +33,18 @@ export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInf
options.success?.(res) options.success?.(res)
options.complete?.(res) options.complete?.(res)
} else { } else {
const res = new UniError("uni-getBatteryInfo", 1001, "getBatteryInfo:fail getAppContext is null") let res = new GetBatteryInfoFailImpl(1001);
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;
...@@ -65,7 +61,6 @@ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryIn ...@@ -65,7 +61,6 @@ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryIn
level: level, level: level,
isCharging: isCharging isCharging: isCharging
}; };
return res; return res;
} }
else { else {
...@@ -78,6 +73,4 @@ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryIn ...@@ -78,6 +73,4 @@ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryIn
}; };
return res; return res;
} }
} }
...@@ -4,10 +4,9 @@ import { UIDevice } from "UIKit"; ...@@ -4,10 +4,9 @@ import { UIDevice } from "UIKit";
import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts'; import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts';
/** /**
* 导出 获取电量方法 * 异步获取电量
*/ */
export const getBatteryInfo : GetBatteryInfo = function (options) { export const getBatteryInfo : GetBatteryInfo = function (options) {
// 开启电量检测 // 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true UIDevice.current.isBatteryMonitoringEnabled = true
...@@ -21,8 +20,10 @@ export const getBatteryInfo : GetBatteryInfo = function (options) { ...@@ -21,8 +20,10 @@ export const getBatteryInfo : GetBatteryInfo = function (options) {
options.complete?.(res); options.complete?.(res);
} }
export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult { /**
* 同步获取电量
*/
export const getBatteryInfoSync : GetBatteryInfoSync = function () : GetBatteryInfoResult {
// 开启电量检测 // 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true UIDevice.current.isBatteryMonitoringEnabled = true
......
...@@ -10,27 +10,6 @@ export type GetBatteryInfoSuccess = { ...@@ -10,27 +10,6 @@ export type GetBatteryInfoSuccess = {
isCharging : boolean isCharging : boolean
} }
// export type GetBatteryInfoFail = {
// /**
// * 错误码
// */
// errCode : number,
// /**
// * 调用API的名称
// */
// errSubject : string,
// /**
// * 错误的详细信息
// */
// errMsg : string,
// /**
// * 错误来源
// */
// cause : any | null
// }
export type GetBatteryInfoOptions = { export type GetBatteryInfoOptions = {
/** /**
* 接口调用结束的回调函数(调用成功、失败都会执行) * 接口调用结束的回调函数(调用成功、失败都会执行)
...@@ -57,6 +36,18 @@ export type GetBatteryInfoResult = { ...@@ -57,6 +36,18 @@ export type GetBatteryInfoResult = {
isCharging : boolean isCharging : boolean
} }
/**
* 错误码
* - 1001 getAppContext is null
*/
export type GetBatteryInfoErrorCode = 1001 ;
/**
* GetBatteryInfo 的错误回调参数
*/
export interface GetBatteryInfoFail extends IUniError {
errCode : GetBatteryInfoErrorCode
};
/** /**
* 获取电量信息 * 获取电量信息
* @param {GetBatteryInfoOptions} options * @param {GetBatteryInfoOptions} options
...@@ -98,10 +89,14 @@ interface Uni { ...@@ -98,10 +89,14 @@ interface Uni {
* "unixVer": "3.9.0" * "unixVer": "3.9.0"
* }, * },
* "ios": { * "ios": {
* "osVer": "9.0", * "osVer": "12.0",
* "uniVer": "3.6.11", * "uniVer": "3.6.11",
* "unixVer": "3.9.0" * "unixVer": "4.11"
* } * }
* },
* "web": {
* "uniVer": "3.6.11",
* "unixVer": "4.0"
* } * }
* } * }
* @uniVueVersion 2,3 //支持的vue版本 * @uniVueVersion 2,3 //支持的vue版本
...@@ -125,10 +120,14 @@ interface Uni { ...@@ -125,10 +120,14 @@ interface Uni {
* "unixVer": "3.9.0" * "unixVer": "3.9.0"
* }, * },
* "ios": { * "ios": {
* "osVer": "9.0", * "osVer": "12.0",
* "uniVer": "3.6.11", * "uniVer": "3.6.11",
* "unixVer": "3.9.0" * "unixVer": "4.11"
* } * }
* },
* "web": {
* "uniVer": "3.6.11",
* "unixVer": "4.0"
* } * }
* } * }
* @uniVueVersion 2,3 //支持的vue版本 * @uniVueVersion 2,3 //支持的vue版本
......
import { GetBatteryInfoErrorCode, GetBatteryInfoFail } from "./interface.uts"
/**
* 错误主题
*/
export const UniErrorSubject = 'uni-getBatteryInfo';
/**
* 错误信息
* @UniError
*/
export const UniErrors : Map<GetBatteryInfoErrorCode, string> = new Map([
/**
* 错误码及对应的错误信息
*/
[1001, 'getBatteryInfo:fail getAppContext is null'],
]);
/**
* 错误对象实现
*/
export class GetBatteryInfoFailImpl extends UniError implements GetBatteryInfoFail {
/**
* 错误对象构造函数
*/
constructor(errCode : GetBatteryInfoErrorCode) {
super();
this.errSubject = UniErrorSubject;
this.errCode = errCode;
this.errMsg = UniErrors[errCode] ?? "";
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册