吉安感知网项目-前端
chenyao
2026-02-11 e4439e5d0a2cba9983f013bd044fe4e22c7b7077
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import Build from 'android.os.Build'
import Context from 'android.content.Context'
import Intent from 'android.content.Intent'
import Settings from 'android.provider.Settings'
import { UTSAndroid } from 'io.dcloud.uts'
import BatteryManager from "android.os.BatteryManager";
 
// 拉起应用
export function openDialog(): void {
    const context = UTSAndroid.getAppContext()
    if (context == null) return
    try {
        const pm = context.getPackageManager()
        const intent = pm.getLaunchIntentForPackage(context.getPackageName())
        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    } catch (e) {
        console.error(e)
    }
}
 
// 跳到允许悬浮窗权限页面
export function allowFloat() {
    const context = UTSAndroid.getAppContext()
    if (context == null) return
    // 1. 检查悬浮窗权限 (Android 6.0+)
    if (Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(context)) {
        try {
            // 无权限,跳转到权限设置页面
            const intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
            intent.setData(android.net.Uri.parse('package:' + context.getPackageName()))
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        } catch (e) {
            console.error(e)
        }
        return
    }
}
 
/**
 * 获取电池电量百分比
 *
 * uni-app项目中调用示例:
 * import { getBatteryCapacity } from "@/uni_modules/lgh-getbatteryinfo"
 * const capacity = getBatteryCapacity()
 * console.log('电池电量:', capacity)
 */
export function getBatteryCapacity(): string {
    // 获取android系统 application上下文
    const context = UTSAndroid.getAppContext();
    if (context != null) {
        const manager = context.getSystemService(
            Context.BATTERY_SERVICE
        ) as BatteryManager;
        const currentLevel: number = manager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CAPACITY
        );
        return '' + currentLevel + '%';
    }
    return "0%";
}