index.uts 2.6 KB
Newer Older
1 2 3
import Context from "android.content.Context";
import BatteryManager from "android.os.BatteryManager";

DCloud-yyl's avatar
DCloud-yyl 已提交
4
import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts'
杜庆泉's avatar
杜庆泉 已提交
5 6
import IntentFilter from 'android.content.IntentFilter';
import Intent from 'android.content.Intent';
DCloud-yyl's avatar
DCloud-yyl 已提交
7 8


9 10 11
/**
 * 异步获取电量
 */
杜庆泉's avatar
杜庆泉 已提交
12 13 14
export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) {


15 16 17 18 19 20 21
    const context = UTSAndroid.getAppContext();
    if (context != null) {
        const manager = context.getSystemService(
            Context.BATTERY_SERVICE
        ) as BatteryManager;
        const level = manager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CAPACITY
杜庆泉's avatar
杜庆泉 已提交
22 23 24 25 26 27 28
        );

        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;

29 30 31
        const res : GetBatteryInfoSuccess = {
            errMsg: 'getBatteryInfo:ok',
            level,
杜庆泉's avatar
杜庆泉 已提交
32
            isCharging: isCharging
33 34 35 36 37 38 39 40 41 42 43 44 45 46
        }
        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)
    }
}

/**
 * 同步获取电量示例
 */
export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult {
杜庆泉's avatar
杜庆泉 已提交
47

48
    const context = UTSAndroid.getAppContext();
杜庆泉's avatar
杜庆泉 已提交
49 50 51
    if (context != null) {


52 53 54 55 56 57
        const manager = context.getSystemService(
            Context.BATTERY_SERVICE
        ) as BatteryManager;
        const level = manager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CAPACITY
        );
杜庆泉's avatar
杜庆泉 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        
        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;
80
    }
杜庆泉's avatar
杜庆泉 已提交
81 82 83


}