how to show my screen alarm from background manager
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.
1. Modifying plugin
EDIT: To open an app when an alarm goes off, Need to modify the plugin.
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.
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. Permissions
There is a need for permission to run the app when the alarm goes off from the background.
- Display over other apps
- Ignore battery optimization
You can access permissions by using permission_handler.
Ahmed Magdy
Updated on December 29, 2022Comments
-
Ahmed Magdy over 1 year
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! "); }
-
Ahmed Magdy about 3 yearsThis is very good. Thanks for your effort, I will try it
-
Ahmed Magdy about 3 yearsPlease, can you explain to me in the project that you have set, Where is the exact point from which I can open the application, except for cases and data
-
MaNDOOoo about 3 years@ahmed Magdy An app will open by modified
AlarmBroadcastReceiver.java
. When the scheduled time comes, that receiver will run and open the app. See theIntent
.