如何从后台管理器显示我的屏幕警报 [英] how to show my screen alarm from background manager

查看:56
本文介绍了如何从后台管理器显示我的屏幕警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了 android 警报管理器包并在后台打印代码,它运行良好.但是如何显示我的警报屏幕,例如 WhatsApp 接听电话,我可以用颤振来做到这一点吗?

I installed android alarm manager package and print code on background its work well. But how can show my alarm screen like WhatsApp receive call for example, can I do this with 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 找到了这个解决方案.它使得可以从后台显示应用程序.

Well in my case, I modified the plugin code and needed some permission. I found this solution from geisterfurz007/random-alarm. It makes can show the app from the background.

要在闹钟响起时打开应用程序,需要修改插件.

To open an app when an alarm goes off, Need to modify the plugin.

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

Open the project and find the android_alarm_manager-2.0.0 in the External Libraries/Flutter Plugins. And find AlarmBroadcastReceiver.java, copy-paste the following code. That code is from the flutter issue.

// 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.权限

当闹钟从后台响起时,需要获得权限才能运行应用程序.

2. Permissions

There is a need for permission to run the app when the alarm goes off from the background.

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

您可以使用 permission_handler 访问权限.

You can access permissions by using permission_handler.

这篇关于如何从后台管理器显示我的屏幕警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆