提交 c95a0c0e 编写于 作者: 杜庆泉's avatar 杜庆泉

uniappx modules 修改同步到获取电量主仓库

上级 ace59dd7
...@@ -2,12 +2,16 @@ import Context from "android.content.Context"; ...@@ -2,12 +2,16 @@ 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 Intent from 'android.content.Intent';
/** /**
* 异步获取电量 * 异步获取电量
*/ */
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(
...@@ -16,20 +20,20 @@ export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInf ...@@ -16,20 +20,20 @@ export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInf
const level = manager.getIntProperty( const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY 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 = { const res : GetBatteryInfoSuccess = {
errMsg: 'getBatteryInfo:ok', errMsg: 'getBatteryInfo:ok',
level, level,
isCharging: manager.isCharging() isCharging: isCharging
} }
options.success?.(res) options.success?.(res)
options.complete?.(res) options.complete?.(res)
} else { } 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") const res = new UniError("uni-getBatteryInfo", 1001, "getBatteryInfo:fail getAppContext is null")
options.fail?.(res) options.fail?.(res)
options.complete?.(res) options.complete?.(res)
...@@ -43,6 +47,8 @@ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryIn ...@@ -43,6 +47,8 @@ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryIn
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;
...@@ -50,12 +56,19 @@ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryIn ...@@ -50,12 +56,19 @@ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryIn
BatteryManager.BATTERY_PROPERTY_CAPACITY 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 = { const res : GetBatteryInfoResult = {
level: level, level: level,
isCharging: manager.isCharging() isCharging: isCharging
}; };
return res; return res;
} else { }
else {
/** /**
* 无有效上下文 * 无有效上下文
*/ */
......
{
"deploymentTarget": "9"
}
\ No newline at end of file
...@@ -38,5 +38,5 @@ declare interface Uni { ...@@ -38,5 +38,5 @@ declare interface Uni {
/** /**
* 同步获取电池电量信息 * 同步获取电池电量信息
*/ */
GetBatteryInfoSync(): UniNamespace.GetBatteryInfoSuccessCallbackResult; getBatteryInfoSync(): UniNamespace.GetBatteryInfoSuccessCallbackResult;
} }
...@@ -107,7 +107,7 @@ interface Uni { ...@@ -107,7 +107,7 @@ interface Uni {
* @uniVueVersion 2,3 //支持的vue版本 * @uniVueVersion 2,3 //支持的vue版本
* *
*/ */
getBatteryInfo(options : GetBatteryInfoOptions) : void, getBatteryInfo (options : GetBatteryInfoOptions) : void,
/** /**
* 同步获取电池电量信息 * 同步获取电池电量信息
* @description 获取电池电量信息 * @description 获取电池电量信息
...@@ -134,6 +134,6 @@ interface Uni { ...@@ -134,6 +134,6 @@ interface Uni {
* @uniVueVersion 2,3 //支持的vue版本 * @uniVueVersion 2,3 //支持的vue版本
* *
*/ */
getBatteryInfoSync() : GetBatteryInfoResult getBatteryInfoSync():GetBatteryInfoResult
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册