提交 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();
if (context != null) {
const manager = context.getSystemService(
Context.BATTERY_SERVICE
) as BatteryManager;
const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const context = UTSAndroid.getAppContext(); const res : GetBatteryInfoSuccess = {
if (context != null) { errMsg: 'getBatteryInfo:ok',
const manager = context.getSystemService( level,
Context.BATTERY_SERVICE isCharging: isCharging
) as BatteryManager;
const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const res : GetBatteryInfoSuccess = {
errMsg: 'getBatteryInfo:ok',
level,
isCharging: isCharging
}
options.success?.(res)
options.complete?.(res)
} else {
const res = new UniError("uni-getBatteryInfo", 1001, "getBatteryInfo:fail getAppContext is null")
options.fail?.(res)
options.complete?.(res)
} }
options.success?.(res)
options.complete?.(res)
} else {
let res = new GetBatteryInfoFailImpl(1001);
options.fail?.(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(
Context.BATTERY_SERVICE
) as BatteryManager;
const manager = context.getSystemService( const level = manager.getIntProperty(
Context.BATTERY_SERVICE BatteryManager.BATTERY_PROPERTY_CAPACITY
) as BatteryManager; );
const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const res : GetBatteryInfoResult = {
level: level,
isCharging: isCharging
};
return res;
}
else {
/**
* 无有效上下文
*/
const res : GetBatteryInfoResult = {
level: -1,
isCharging: false
};
return res;
}
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const res : GetBatteryInfoResult = {
level: level,
isCharging: isCharging
};
return res;
}
else {
/**
* 无有效上下文
*/
const res : GetBatteryInfoResult = {
level: -1,
isCharging: false
};
return res;
}
} }
// 引用 iOS 原生平台 api // 引用 iOS 原生平台 api
import { UIDevice } from "UIKit"; 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
// 返回数据
// 返回数据 const res : GetBatteryInfoSuccess = {
const res : GetBatteryInfoSuccess = { errMsg: "getBatteryInfo:ok",
errMsg: "getBatteryInfo:ok", level: Math.abs(Number(UIDevice.current.batteryLevel * 100)),
level: Math.abs(Number(UIDevice.current.batteryLevel * 100)), isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging, };
}; options.success?.(res);
options.success?.(res); 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
// 返回数据
const res : GetBatteryInfoResult = { // 返回数据
level: Math.abs(Number(UIDevice.current.batteryLevel * 100)), const res : GetBatteryInfoResult = {
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging, level: Math.abs(Number(UIDevice.current.batteryLevel * 100)),
}; isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
return res; };
return res;
} }
export type GetBatteryInfoSuccess = { export type GetBatteryInfoSuccess = {
errMsg : string, errMsg : string,
/** /**
* 设备电量,范围1 - 100 * 设备电量,范围1 - 100
*/ */
level : number, level : number,
/** /**
* 是否正在充电中 * 是否正在充电中
*/ */
isCharging : boolean isCharging : boolean
} }
// export type GetBatteryInfoFail = { export type GetBatteryInfoOptions = {
// /** /**
// * 错误码 * 接口调用结束的回调函数(调用成功、失败都会执行)
// */ */
// errCode : number, success ?: (res : GetBatteryInfoSuccess) => void
// /** /**
// * 调用API的名称 * 接口调用失败的回调函数
// */ */
// errSubject : string, fail ?: (res : UniError) => void
// /** /**
// * 错误的详细信息 * 接口调用成功的回调
// */ */
// errMsg : string, complete ?: (res : any) => void
// /** }
// * 错误来源
// */ export type GetBatteryInfoResult = {
// cause : any | null /**
// } * 设备电量,范围1 - 100
*/
level : number,
/**
export type GetBatteryInfoOptions = { * 是否正在充电中
/** */
* 接口调用结束的回调函数(调用成功、失败都会执行) isCharging : boolean
*/
success ?: (res : GetBatteryInfoSuccess) => void
/**
* 接口调用失败的回调函数
*/
fail ?: (res : UniError) => void
/**
* 接口调用成功的回调
*/
complete ?: (res : any) => void
} }
export type GetBatteryInfoResult = { /**
/** * 错误码
* 设备电量,范围1 - 100 * - 1001 getAppContext is null
*/ */
level : number, export type GetBatteryInfoErrorCode = 1001 ;
/** /**
* 是否正在充电中 * GetBatteryInfo 的错误回调参数
*/ */
isCharging : boolean export interface GetBatteryInfoFail extends IUniError {
} errCode : GetBatteryInfoErrorCode
};
/**
* 获取电量信息 /**
* @param {GetBatteryInfoOptions} options * 获取电量信息
* * @param {GetBatteryInfoOptions} options
* *
* @tutorial https://uniapp.dcloud.net.cn/api/system/batteryInfo.html *
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22 * @tutorial https://uniapp.dcloud.net.cn/api/system/batteryInfo.html
* @since 3.6.11 * @platforms APP-IOS = ^9.0,APP-ANDROID = ^22
* * @since 3.6.11
* @assert () => success({errCode: 0, errSubject: "uni-getBatteryInfo", errMsg: "getBatteryInfo:ok", level: 60, isCharging: false }) *
* @assert () => fail({errCode: 1001, errSubject: "uni-getBatteryInfo", errMsg: "getBatteryInfo:fail getAppContext is null" }) * @assert () => success({errCode: 0, errSubject: "uni-getBatteryInfo", errMsg: "getBatteryInfo:ok", level: 60, isCharging: false })
*/ * @assert () => fail({errCode: 1001, errSubject: "uni-getBatteryInfo", errMsg: "getBatteryInfo:fail getAppContext is null" })
*/
export type GetBatteryInfo = (options : GetBatteryInfoOptions) => void export type GetBatteryInfo = (options : GetBatteryInfoOptions) => void
export type GetBatteryInfoSync = () => GetBatteryInfoResult export type GetBatteryInfoSync = () => GetBatteryInfoResult
interface Uni { interface Uni {
/** /**
* 获取电池电量信息 * 获取电池电量信息
* @description 获取电池电量信息 * @description 获取电池电量信息
* @param {GetBatteryInfoOptions} options * @param {GetBatteryInfoOptions} options
* @example * @example
* ```typescript * ```typescript
* uni.getBatteryInfo({ * uni.getBatteryInfo({
* success(res) { * success(res) {
* console.log(res); * console.log(res);
* } * }
* }) * })
* ``` * ```
* @remark * @remark
* - 该接口需要同步调用 * - 该接口需要同步调用
* @uniPlatform { * @uniPlatform {
* "app": { * "app": {
* "android": { * "android": {
* "osVer": "4.4.4", * "osVer": "4.4.4",
* "uniVer": "3.6.11", * "uniVer": "3.6.11",
* "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": {
* @uniVueVersion 2,3 //支持的vue版本 * "uniVer": "3.6.11",
* * "unixVer": "4.0"
*/ * }
* }
* @uniVueVersion 2,3 //支持的vue版本
*
*/
getBatteryInfo (options : GetBatteryInfoOptions) : void, getBatteryInfo (options : GetBatteryInfoOptions) : void,
/** /**
* 同步获取电池电量信息 * 同步获取电池电量信息
...@@ -125,15 +120,19 @@ interface Uni { ...@@ -125,15 +120,19 @@ 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版本
* *
*/ */
getBatteryInfoSync():GetBatteryInfoResult getBatteryInfoSync():GetBatteryInfoResult
} }
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.
先完成此消息的编辑!
想要评论请 注册