| | |
| | | import PowerManager from 'android.os.PowerManager' |
| | | import BatteryManager from 'android.os.BatteryManager' |
| | | import { UTSAndroid } from 'io.dcloud.uts' |
| | | import AlertDialog from 'android.app.AlertDialog' |
| | | import DialogInterface from 'android.content.DialogInterface' |
| | | |
| | | // 保活服务的通知渠道ID |
| | | const CHANNEL_ID = 'keep_alive_channel' |
| | |
| | | return '0%' |
| | | } |
| | | |
| | | /** |
| | | * 请求忽略电池优化(需要用户手动确认) |
| | | * 调用此方法会弹出系统设置页面 |
| | | */ |
| | | export function requestIgnoreBatteryOptimization(): void { |
| | | const context = UTSAndroid.getAppContext() |
| | | if (context == null) { |
| | | return |
| | | } |
| | | |
| | | if (Build.VERSION.SDK_INT >= 23) { |
| | | // Android 6.0+ |
| | | const powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager |
| | | const packageName = context.getPackageName() |
| | | |
| | | if (!powerManager.isIgnoringBatteryOptimizations(packageName)) { |
| | | try { |
| | | const intent = new Intent() |
| | | intent.setAction('android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS') |
| | | intent.setData(android.net.Uri.parse('package:' + packageName)) |
| | | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| | | context.startActivity(intent) |
| | | } catch (e) { |
| | | console.error('requestIgnoreBatteryOptimization: 打开设置失败', e) |
| | | } |
| | | } else { |
| | | console.log('requestIgnoreBatteryOptimization: 已在白名单中') |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 检查是否已忽略电池优化 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 请求忽略电池优化(需要用户手动确认) |
| | | * 调用此方法会弹出系统设置页面 |
| | | */ |
| | | export function requestIgnoreBatteryOptimization(): void { |
| | | if (isIgnoringBatteryOptimizations()) return |
| | | const activity = UTSAndroid.getUniActivity() |
| | | if (activity == null) return |
| | | AlertDialog.Builder(activity) |
| | | .setTitle('权限提示') |
| | | .setMessage('为了在后台收到来电时能弹出通话界面,请将应用添加到电池优化白名单') |
| | | .setPositiveButton('去设置', (_dialog : DialogInterface, _which : Int) => { |
| | | const context = UTSAndroid.getAppContext() |
| | | if (context == null) return |
| | | if (Build.VERSION.SDK_INT >= 23) { |
| | | const powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager |
| | | const packageName = context.getPackageName() |
| | | if (!powerManager.isIgnoringBatteryOptimizations(packageName)) { |
| | | try { |
| | | const intent = new Intent() |
| | | intent.setAction('android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS') |
| | | intent.setData(android.net.Uri.parse('package:' + packageName)) |
| | | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| | | context.startActivity(intent) |
| | | } catch (e) { |
| | | console.error('requestIgnoreBatteryOptimization: 打开设置失败', e) |
| | | } |
| | | } |
| | | } |
| | | }) |
| | | .setNegativeButton('暂不', null) |
| | | .show() |
| | | } |
| | | |
| | | /** |
| | | * 创建通知渠道 (Android 8.0+ 必需) |
| | | */ |
| | | function createNotificationChannel(context: Context): void { |