After importing this plugin to your project as usual, add the following to your
AndroidManifest.xml within the
Next, within the
<application></application> tags, add:
Then in Dart code add:
printHello will then run (roughly) every minute, even if the main app ends. However,
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
If alarm callbacks will need access to other Flutter plugins, including the alarm manager plugin itself, it may be necessary to inform the background service how to initialize plugins depending on which Flutter Android embedding the application is using.
For the Flutter Android Embedding V2, plugins are registered with the background
isolate via reflection so
AlarmService.setPluginRegistrant does not need to be
NOTE: this plugin is not completely compatible with the V2 embedding on
Flutter versions < 1.12 as the background isolate will not automatically
register plugins. This can be resolved by running
flutter upgrade to upgrade
to the latest Flutter version.
For the Flutter Android Embedding V1, the background service must be provided a
callback to register plugins with the background isolate. This is done by giving
AlarmService a callback to call the application's
onCreate method. See the example's
In particular, its
Application class is as follows:
Which must be reflected in the application's
Note: Not calling
AlarmService.setPluginRegistrant will result in an exception being
thrown when an alarm eventually fires.
Run normally with
flutter test from the root of the project.
The Espresso test runs the same sample code provided in
but is run using the Flutter Espresso plugin.
main.dart will cause this test to fail.
This test will call into the
example/lib/main_espresso.dart file which
will enable Flutter Driver and then calls into the
See https://pub.dev/packages/espresso for more info on why.
To run the test, run from the
To run the Flutter Driver tests, cd into
example and run: