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


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


fxy060608's avatar
fxy060608 已提交
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
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;

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

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

48
    const context = UTSAndroid.getAppContext();
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
        );
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
    }
81 82 83


}