【问题标题】:Error while trying to bring system_alert_window in android using flutter when app is closed尝试在关闭应用程序时使用颤振将 system_alert_window 引入 android 时出错
【发布时间】:2022-01-03 07:00:57
【问题描述】:

我厌倦了使用android_alarm_manager_plus 在特定时间安排后台事件来解决这个问题。而且到目前为止效果很好,但是为了显示弹出窗口,我使用了来自pub.dev 的包system_alert_window。只要应用程序在后台打开,弹出窗口就会工作并显示为叠加层。但是当应用关闭时触发警报事件时,它会弹出您的应用已崩溃

总结一下:只要应用程序打开,system_alert_window 就可以正常工作。但是当应用关闭时调用时,它会崩溃。

请有人帮助我解决这个问题。

用于测试 11 和 12 的 Android 版本

权限列表

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE " />
    <!-- For apps with targetSDK=31 (Android 12) -->
    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>

用于打开弹出窗口的代码

import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:system_alert_window/system_alert_window.dart' as alw;
import './system_alert.dart';

bool _alarmStatus = false;
  static void printHello() {
    final DateTime now = DateTime.now();
    final int isolateId = Isolate.current.hashCode;
    alw.SystemAlertWindow.showSystemWindow(
      height: 230,
      header: header,
      body: body,
      footer: footer,
      margin: alw.SystemWindowMargin(left: 8, right: 8, top: 100, bottom: 0),
      gravity: alw.SystemWindowGravity.TOP,
      notificationTitle: "Incoming Call",
      notificationBody: "+1 646 980 4741",
      prefMode: alw.SystemWindowPrefMode.OVERLAY,
    );
    AndroidAlarmManager.cancel(696969);
    AndroidAlarmManager.oneShot(
      const Duration(seconds: 20),
      20070,
      printHello,
    );
  }

  void _setAlarm() async {
    final int helloAlarmID = 696969;
    await AndroidAlarmManager.oneShot(
      const Duration(seconds: 15),
      helloAlarmID,
      printHello,
    );
    setState(() {
      _alarmStatus = !_alarmStatus;
    });
  }

崩溃日志 -

01-10 01:07:54.195 3397 3397 E AndroidRuntime: at dev.fluttercommunity.plus.androidalarmmanager.AlarmService.onCreate(AlarmService.java:351) 
01-10 01:07:54.195 3397 3397 E AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:4307) 
01-10 01:07:54.195 3397 3397 E AndroidRuntime: ... 8 more 
01-10 01:07:54.201 2225 3489 I DropBoxManagerService: add tag=data_app_crash isTagEnabled=true flags=0x2 01-10 
01:07:54.211 2225 2324 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.DROPBOX_ENTRY_ADDED flg=0x10 (has extras) } to com.google.android.gms/.stats.service.DropBoxEntryAddedReceiver 
01-10 01:07:54.212 2225 2324 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.DROPBOX_ENTRY_ADDED flg=0x10 (has extras) } to com.google.android.gms/.chimera.GmsIntentOperationService$PersistentTrustedReceiver

【问题讨论】:

  • 是指您想在您的应用处于非活动状态或后台时显示弹出窗口?
  • 几乎可以。我要做。如果应用程序在后台运行,则没有错误。但是,当我尝试在没有后台运行应用程序的情况下执行相同操作时,代码会崩溃。

标签: android flutter dart flutter-dependencies


【解决方案1】:

您是否将其添加到您的 AndroidManifest.xml 文件中 application tag

android:name = "io.flutter.app.FlutterApplication"

【讨论】:

  • 是的,我添加了
  • 你在Application Class中添加了需要的东西吗?
  • 是的,我已经添加了
  • O,我刚刚有了一个新发现。它在 Android 11 和 12 中不起作用。在较低版本中工作正常
【解决方案2】:
 AndroidAlarmManager.oneShot(
      Duration(seconds: 10),
      0,
      printHello,
      wakeup: true,
    ).then((val) => print(val));

唤醒:真

【讨论】:

  • 是的,已经试过了。它仍然崩溃。在问题中添加了崩溃日志
  • O,我刚刚有了一个新发现。它在 Android 11 和 12 中不起作用。在较低版本中工作正常
  • 那么可能是权限问题,请允许手机权限,然后重试。
猜你喜欢
  • 2020-03-22
  • 1970-01-01
  • 2022-01-25
  • 2021-08-23
  • 2020-04-20
  • 2019-10-16
  • 2022-10-06
  • 1970-01-01
  • 2019-03-05
相关资源
最近更新 更多