index.uts 2.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
import Context from "android.content.Context";
import BatteryManager from "android.os.BatteryManager";

4
import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts'
5 6
import IntentFilter from 'android.content.IntentFilter';
import Intent from 'android.content.Intent';
7

8
import { GetBatteryInfoFailImpl } from '../unierror';
9

10 11 12
/**
 * 异步获取电量
 */
13 14 15
export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) {


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

        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;

fxy060608's avatar
fxy060608 已提交
30 31 32
        const res : GetBatteryInfoSuccess = {
            errMsg: 'getBatteryInfo:ok',
            level,
33
            isCharging: isCharging
fxy060608's avatar
fxy060608 已提交
34 35 36
        }
        options.success?.(res)
        options.complete?.(res)
37 38
    } else {
		let res = new GetBatteryInfoFailImpl(1001);
fxy060608's avatar
fxy060608 已提交
39 40 41
        options.fail?.(res)
        options.complete?.(res)
    }
42 43 44 45 46 47
}

/**
 * 同步获取电量示例
 */
export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult {
48

49
    const context = UTSAndroid.getAppContext();
50 51 52
    if (context != null) {


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


}