android_alarm_manager 0.1.0

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

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.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. If possible it will reuse the same Dart Isolate from the application's main activity. Additionally, if you would like a new main activity of your application to reuse the Isolate from an existing background service created by this plugin, add the following override to your app's MainActivity class:

@Override
public FlutterNativeView createFlutterNativeView() {
  return AlarmService.getSharedFlutterView();
}

See the example's MainActivity to see an example.

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.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;
  printHelloMessage("Hello, main()!");
  runApp(const Center(
      child: const Text('Hello, world!', textDirection: TextDirection.ltr)));
  await AndroidAlarmManager.periodic(
      const Duration(minutes: 1), helloAlarmID, printHello);
  await AndroidAlarmManager.periodic(
      const Duration(minutes: 1), goodbyeAlarmID, printGoodbye);
  if (!oneShotFired) {
    await AndroidAlarmManager.oneShot(
        const Duration(minutes: 1), oneShotID, printOneShot);
  }
}

1. Depend on it

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


dependencies:
  android_alarm_manager: "^0.1.0"

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support 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.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

Analysis

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

  • Dart: 2.0.0-dev.49.0
  • pana: 0.10.6
  • Flutter: 0.3.2

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
84 / 100
Health:
Code health derived from static analysis. [more]
100 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
92
Learn more about scoring.

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

  • 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 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.9
meta 1.1.2
sky_engine 0.0.99
typed_data 1.1.5
vector_math 2.0.6