android_alarm_manager 0.2.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 43

android_alarm_manager

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:

<service
    android:name="io.flutter.plugins.androidalarmmanager.AlarmService"
    android:exported="false"/>

Then in Dart code add:

import 'package:android_alarm_manager/android_alarm_manager.dart';

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

main() async {
  final int helloAlarmID = 0;
  runApp(...);
  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 {
  @Override
  public void onCreate() {
    super.onCreate();
    AlarmService.setPluginRegistrant(this);
  }

  @Override
  public void registerWith(PluginRegistry registry) {
    GeneratedPluginRegistrant.registerWith(registry);
  }
}

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

    <application
        android:name=".Application"
        ...

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

For help on editing plugin code, view the documentation.

0.2.0

  • 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.

0.1.1

  • Updated Gradle tooling to match Android Studio 3.1.2.

0.1.0

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

0.0.5

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

0.0.4

  • 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.

0.0.3

  • 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.

0.0.2

  • Add FLT prefix to iOS types.

0.0.1

  • Initial release.

example/lib/main.dart

// 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);

  @override
  String toString() {
    return "[$_now] $_msg "
        "isolate=$_isolate "
        "user='$_user' "
        "token=$_token";
  }
}

void printHelloMessage(String msg) {
  ensureFirebaseUser().then((_) {
    firebaseUser.getIdToken().then((String idToken) {
      print(new HelloMessage(
        new DateTime.now(),
        msg,
        Isolate.current.hashCode,
        firebaseUser,
        idToken,
      ));
    });
  });
}

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:


dependencies:
  android_alarm_manager: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or 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.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
Popularity:
Describes how popular the package is relative to other packages. [more]
86
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
43
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.11.8
  • Flutter: 0.5.7

Platforms

Detected platforms: unsure

Low code quality prevents platform classification.

Issues and suggestions

Fix lib/android_alarm_manager.dart.

Analysis of lib/android_alarm_manager.dart failed with 9 errors, 1 hint, including:

line 31 col 11: Undefined class 'CallbackHandle'.

line 31 col 39: Undefined class 'CallbackHandle.fromRawHandle'.

line 35 col 30: Undefined name 'PluginUtilities'.

line 64 col 11: Undefined class 'CallbackHandle'.

line 65 col 9: Undefined name 'PluginUtilities'.

Fix platform conflicts.

Low code quality prevents platform classification.

Package is pre-v1 release.

While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.6 1.14.11
meta 1.1.5 1.1.6
sky_engine 0.0.99
typed_data 1.1.5 1.1.6
vector_math 2.0.6 2.0.8