如何从后台管理器显示我的屏幕警报

how to show my screen alarm from background manager

我安装了 android 警报管理器包并在后台打印代码,它运行良好。 但是如何显示我的闹钟屏幕,例如 WhatsApp 接听电话, 我可以用 flutter 做这个吗?

void runOnBackGround() async {
  final int helloAlarmID = 0;
  await AndroidAlarmManager.initialize();
  await AndroidAlarmManager.periodic(
      const Duration(seconds: 1), helloAlarmID, callBack,
      wakeup: true);
}

void callBack(i) async {
  final DateTime now = DateTime.now();
  print("[$now] id = $i Hello, world! ");
}

嗯,就我而言,我修改了插件代码并需要一些许可。我从 geisterfurz007/random-alarm 找到了这个解决方案。它使得可以从后台显示应用程序。

1。修改插件

编辑:要在闹钟响起时打开应用程序,需要修改插件。

打开项目,在External Libraries/Flutter Plugins中找到android_alarm_manager-2.0.0。并找到 AlarmBroadcastReceiver.java,复制粘贴以下代码。该代码来自 the flutter issue.

AlarmBroadcastReceiver.java

// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
     
package io.flutter.plugins.androidalarmmanager;
     
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.content.pm.PackageManager;
import android.view.WindowManager;
     
public class AlarmBroadcastReceiver extends BroadcastReceiver {
  private static PowerManager.WakeLock wakeLock;
     
  @Override
  public void onReceive(Context context, Intent intent) {
    PowerManager powerManager = (PowerManager)
            context.getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
            PowerManager.ACQUIRE_CAUSES_WAKEUP |
            PowerManager.ON_AFTER_RELEASE, "My wakelock");
     
    Intent startIntent = context
            .getPackageManager()
            .getLaunchIntentForPackage(context.getPackageName());
     
    startIntent.setFlags(
            Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
                    Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
    );
     
    wakeLock.acquire();
    context.startActivity(startIntent);
    AlarmService.enqueueAlarmProcessing(context, intent);
    wakeLock.release();
  }
}

2。权限

当闹钟从后台响起时,需要运行应用程序的权限。

  • 显示在其他应用程序之上
  • 忽略电池优化

您可以使用 permission_handler 访问权限。