| | |
| | | import Build from "android.os.Build"; |
| | | import Context from "android.content.Context"; |
| | | import WindowManager from "android.view.WindowManager"; |
| | | import TextView from "android.widget.TextView"; |
| | | import Gravity from "android.view.Gravity"; |
| | | import Color from "android.graphics.Color"; |
| | | import PixelFormat from "android.graphics.PixelFormat"; |
| | | import Settings from "android.provider.Settings"; |
| | | import { UTSAndroid } from "io.dcloud.uts"; |
| | | 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"; |
| | | |
| | | let dialogView: TextView | null = null; |
| | | |
| | | // 拉起应用 |
| | | export function openDialog(): void { |
| | | const context = UTSAndroid.getAppContext(); |
| | | if (context == null || dialogView != null) return; |
| | | 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)) { |
| | | console.error("无悬浮窗权限"); |
| | | return; |
| | | } |
| | | |
| | | const wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager; |
| | | if (wm == null) return; |
| | | |
| | | dialogView = new TextView(context); |
| | | dialogView!.setText("系统接听页面"); |
| | | dialogView!.setTextColor(Color.WHITE); |
| | | dialogView!.setTextSize(22.0.toFloat()); |
| | | dialogView!.setGravity(Gravity.CENTER); |
| | | dialogView!.setBackgroundColor(Color.parseColor("#CC000000")); |
| | | |
| | | const type = Build.VERSION.SDK_INT >= 26 ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_PHONE; |
| | | const flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | |
| | | WindowManager.LayoutParams.FLAG_FULLSCREEN | |
| | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | |
| | | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | |
| | | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; |
| | | |
| | | const params = new WindowManager.LayoutParams( |
| | | WindowManager.LayoutParams.MATCH_PARENT, |
| | | WindowManager.LayoutParams.MATCH_PARENT, |
| | | type, |
| | | flags, |
| | | PixelFormat.TRANSLUCENT |
| | | ); |
| | | |
| | | try { |
| | | wm.addView(dialogView, params); |
| | | } catch (e) { |
| | | console.error(e); |
| | | dialogView = null; |
| | | 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 |
| | | } |
| | | } |
| | | |
| | | export function closeDialog(): void { |
| | | if (dialogView == null) return; |
| | | try { |
| | | const context = UTSAndroid.getAppContext(); |
| | | if (context == null) return; |
| | | const wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager; |
| | | wm.removeView(dialogView); |
| | | } catch (e) { |
| | | console.error(e); |
| | | } finally { |
| | | dialogView = null; |
| | | } |
| | | /** |
| | | * 获取电池电量百分比 |
| | | * |
| | | * 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%"; |
| | | } |
| | | |