android_alarm_manager 0.2.1

  • Example
  • Installing
  • Versions
  • 94


pub package

A Flutter plugin for accessing the Android AlarmManager service, and running Dart code in the background when alarms fire.

Getting Started

After importing this plugin to your project as usual, add the following to your AndroidManifest.xml:


Then in Dart code add:

import 'package:android_alarm_manager/android_alarm_manager.dart';

void printHello() {
  final DateTime now = new;
  final int isolateId = Isolate.current.hashCode;
  print("[$now] Hello, world! isolate=${isolateId} function='$printHello'");

main() async {
  final int helloAlarmID = 0;
  await AndroidAlarmManager.periodic(const Duration(minutes: 1), helloAlarmID, printHello);

printHello will then run (roughly) every minute, even if the main app ends. However, printHello will not run in the same isolate as the main application. Unlike threads, isolates do not share memory and communication between isolates must be done via message passing (see more documentation on isolates here).

If alarm callbacks will need access to other Flutter plugins, including the alarm manager plugin itself, it is necessary to teach the background service how to initialize plugins. This is done by giving the AlarmService a callback to call in the application's onCreate method. See the example's Application overrides. In particular, its Application class is as follows:

public class Application extends FlutterApplication implements PluginRegistrantCallback {
  public void onCreate() {

  public void registerWith(PluginRegistry registry) {

Which must be reflected in the application's AndroidManifest.xml. E.g.:


For help getting started with Flutter, view our online documentation.

For help on editing plugin code, view the documentation.


  • Update dependencies for example to point to published versions of firebase_auth and google_sign_in.
  • Add missing dependency on firebase_auth.1


  • Breaking change. A new isolate is always spawned for the background service instead of trying to share an existing isolate owned by the application.
  • Breaking change. Removed AlarmService.getSharedFlutterView.


  • Updated Gradle tooling to match Android Studio 3.1.2.


  • Breaking change. Set SDK constraints to match the Flutter beta release.


  • Simplified and upgraded Android project template to Android SDK 27.
  • Moved Android package to io.flutter.plugins.


  • Breaking change. Upgraded to Gradle 4.1 and Android Studio Gradle plugin 3.0.1. Older Flutter projects need to upgrade their Gradle setup as well in order to use this version of the plugin. Instructions can be found here.


  • Adds use of a Firebase plugin to the example. The example also now demonstrates overriding the Application's onCreate method so that the AlarmService can initialize plugin connections.


  • Add FLT prefix to iOS types.


  • Initial release.


// Copyright 2017 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.

import 'dart:async';
import 'dart:isolate';

import 'package:android_alarm_manager/android_alarm_manager.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/widgets.dart';

final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
FirebaseUser firebaseUser;

Future<Null> ensureFirebaseUser() async {
  if (firebaseUser == null) {
    firebaseUser = await firebaseAuth.currentUser();
    if (firebaseUser == null) {
      firebaseUser = await firebaseAuth.signInAnonymously();

class HelloMessage {
  final DateTime _now;
  final String _msg;
  final int _isolate;
  final FirebaseUser _user;
  final String _token;

  HelloMessage(this._now, this._msg, this._isolate, this._user, this._token);

  String toString() {
    return "[$_now] $_msg "
        "isolate=$_isolate "
        "user='$_user' "

void printHelloMessage(String msg) {
  ensureFirebaseUser().then((_) {
    firebaseUser.getIdToken().then((String idToken) {
      print(new HelloMessage(

void printHello() {
  printHelloMessage("Hello, world!");

void printGoodbye() {
  printHelloMessage("Goodbye, world!");

bool oneShotFired = false;

void printOneShot() {
  printHelloMessage("Hello, once!");

Future<Null> main() async {
  final int helloAlarmID = 0;
  final int goodbyeAlarmID = 1;
  final int oneShotID = 2;

  // Start the AlarmManager service.
  await AndroidAlarmManager.initialize();

  printHelloMessage("Hello, main()!");
  runApp(const Center(
      child: Text('Hello, world!', textDirection: TextDirection.ltr)));
  await AndroidAlarmManager.periodic(
      const Duration(seconds: 5), helloAlarmID, printHello,
      wakeup: true);
  await AndroidAlarmManager.oneShot(
      const Duration(seconds: 5), goodbyeAlarmID, printGoodbye);
  if (!oneShotFired) {
    await AndroidAlarmManager.oneShot(
        const Duration(seconds: 5), oneShotID, printOneShot);

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  android_alarm_manager: ^0.2.1

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:android_alarm_manager/android_alarm_manager.dart';
Version Uploaded Documentation Archive
0.2.1 Aug 21, 2018 Go to the documentation of android_alarm_manager 0.2.1 Download android_alarm_manager 0.2.1 archive
0.2.0 Aug 15, 2018 Go to the documentation of android_alarm_manager 0.2.0 Download android_alarm_manager 0.2.0 archive
0.1.1 Jun 1, 2018 Go to the documentation of android_alarm_manager 0.1.1 Download android_alarm_manager 0.1.1 archive
0.1.0 Mar 9, 2018 Go to the documentation of android_alarm_manager 0.1.0 Download android_alarm_manager 0.1.0 archive
0.0.5 Jan 17, 2018 Go to the documentation of android_alarm_manager 0.0.5 Download android_alarm_manager 0.0.5 archive
0.0.4 Dec 20, 2017 Go to the documentation of android_alarm_manager 0.0.4 Download android_alarm_manager 0.0.4 archive
0.0.2 Dec 3, 2017 Go to the documentation of android_alarm_manager 0.0.2 Download android_alarm_manager 0.0.2 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7
  • Flutter: 1.0.0


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
firebase_auth ^0.5.18 0.5.20 0.6.6
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8