Photo by Sonja Langford on Unsplash

Flutter + AlarmManager = ⏰

tomerpacific
Flutter Community
Published in
3 min readJul 19, 2022

--

If you are an Android developer, when you want to schedule your application to run at a specific time in the future, you use the AlarmManager. If you are an iOS developer, this type of component does not exist there.

If you are a Flutter developer, what do you do?

Like most things related to Flutter, when you want to use a platform specific component, you need to expose its functionality.

AlarmManager is no exception.

This article will go over the Android AlarmManager Plus package and show how you can use it in your application.

Ready to set your alarm?

Setup

  • Open up your pubspec.yaml file and add the following:
dependencies:
android_alarm_manager_plus: ^2.0.6

✋ Disclaimer → When writing this article the latest version was 2.0.6

  • Run pub get to download the dependency

We will be using the vanilla project that you get when you create a Flutter project in Android Studio (minus all the counter logic).

  • Open up your AndroidManifest.xml file and add the following permissions:
  • Inside your application tag, add these as well:

At the end, your AndroidManifest file should look something like this:

AndroidManifest.xml

Alarm Bells Are Ringing

The package exposes an AndroidAlarmManager object that has the following (relevant) methods:

  • oneShot - trigger a one time alarm
  • oneShotAt - trigger a one time alarm at a specific date
  • periodic - trigger an alarm within a defined time interval

Let’s discuss each option in detail.

The oneShot method accepts the following arguments:

oneShot method

The first three arguments (delay, id and callback) are pretty self explanatory so we will focus on the rest.

  • alarmClock - A flag that indicates if the timer will be set with Android’s AlarmManagerCompact.setAlarmClock
  • allowWhileIdle - A flag that indicates if the timer will be set with AlarmManagerCompat.setExactAndAllowWhileIdle or AlarmManagerCompat.setAndAllowWhileIdle
  • exact - A flag that indicates if the timer will be set with AlarmManagerCompat.setExact
  • wakeup - A flag that indicates if the device will be woken up when the alarm will be triggered
  • rescheduleOnReboot - A flag that indicates if the alarm will persist between reboots of the device

The oneShotAt method is very similar to the oneShot method, with one key difference. Instead of a delay of Duration type, the first argument is a DateTime object that sets when the alarm will be triggered.

The periodic method accepts the following arguments:

As you can see, this method is also similar in the arguments it takes. The arguments that matter the most here are:

  • startAt - indicates when the alarm should be first triggered
  • duration - is in charge of retriggering the alarm every duration interval.

Remember To Set Your Alarm

One thing to be aware of regarding the Alarm Manager Plus package is that it uses isolates to run the alarms. Isolates are similar to threads except they don’t share memory. Therefore, they communicate with messages.

Because of this, you must declare your alarm handlers (callbacks) as static so that they can be accessed.

You can read more about isolates here.

--

--