Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
4 changes: 4 additions & 0 deletions packages/android_alarm_manager/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.3

* Added `oneShotAt` method to run `callback` at a given DateTime `time`.

## 0.4.2

* Added support for setting alarms which work when the phone is in doze mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void registerWith(Registrar registrar) {
// alarmManagerPluginChannel is the channel responsible for receiving the following messages
// from the main Flutter app:
// - "AlarmService.start"
// - "Alarm.oneShot"
// - "Alarm.oneShotAt"
// - "Alarm.periodic"
// - "Alarm.cancel"
final MethodChannel alarmManagerPluginChannel =
Expand Down Expand Up @@ -125,7 +125,7 @@ public void onMethodCall(MethodCall call, Result result) {
PeriodicRequest periodicRequest = PeriodicRequest.fromJson((JSONArray) arguments);
AlarmService.setPeriodic(mContext, periodicRequest);
result.success(true);
} else if (method.equals("Alarm.oneShot")) {
} else if (method.equals("Alarm.oneShotAt")) {
// This message indicates that the Flutter app would like to schedule a one-time
// task.
OneShotRequest oneShotRequest = OneShotRequest.fromJson((JSONArray) arguments);
Expand Down
62 changes: 58 additions & 4 deletions packages/android_alarm_manager/lib/android_alarm_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,74 @@ class AndroidAlarmManager {
bool exact = false,
bool wakeup = false,
bool rescheduleOnReboot = false,
}) =>
oneShotAt(
DateTime.now().add(delay),
id,
callback,
alarmClock: alarmClock,
allowWhileIdle: allowWhileIdle,
exact: exact,
wakeup: wakeup,
rescheduleOnReboot: rescheduleOnReboot,
);

/// Schedules a one-shot timer to run `callback` at `time`.
///
/// The `callback` will run whether or not the main application is running or
/// in the foreground. It will run in the Isolate owned by the
/// AndroidAlarmManager service.
///
/// `callback` must be either a top-level function or a static method from a
/// class.
///
/// The timer is uniquely identified by `id`. Calling this function again
/// again with the same `id` will cancel and replace the existing timer.
///
/// If `alarmClock` is passed as `true`, the timer will be created with
/// Android's `AlarmManagerCompat.setAlarmClock`.
///
/// If `allowWhileIdle` is passed as `true`, the timer will be created with
/// Android's `AlarmManagerCompat.setExactAndAllowWhileIdle` or
/// `AlarmManagerCompat.setAndAllowWhileIdle`.
///
/// If `exact` is passed as `true`, the timer will be created with Android's
/// `AlarmManagerCompat.setExact`. When `exact` is `false` (the default), the
/// timer will be created with `AlarmManager.set`.
///
/// If `wakeup` is passed as `true`, the device will be woken up when the
/// alarm fires. If `wakeup` is false (the default), the device will not be
/// woken up to service the alarm.
///
/// If `rescheduleOnReboot` is passed as `true`, the alarm will be persisted
/// across reboots. If `rescheduleOnReboot` is false (the default), the alarm
/// will not be rescheduled after a reboot and will not be executed.
///
/// Returns a [Future] that resolves to `true` on success and `false` on
/// failure.
static Future<bool> oneShotAt(
DateTime time,
int id,
dynamic Function() callback, {
bool alarmClock = false,
bool allowWhileIdle = false,
bool exact = false,
bool wakeup = false,
bool rescheduleOnReboot = false,
}) async {
final int now = DateTime.now().millisecondsSinceEpoch;
final int first = now + delay.inMilliseconds;
final int startMillis = time.millisecondsSinceEpoch;
final CallbackHandle handle = PluginUtilities.getCallbackHandle(callback);
if (handle == null) {
return false;
}
final bool r = await _channel.invokeMethod<bool>('Alarm.oneShot', <dynamic>[
final bool r =
await _channel.invokeMethod<bool>('Alarm.oneShotAt', <dynamic>[
id,
alarmClock,
allowWhileIdle,
exact,
wakeup,
first,
startMillis,
rescheduleOnReboot,
handle.toRawHandle(),
]);
Expand Down
2 changes: 1 addition & 1 deletion packages/android_alarm_manager/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: android_alarm_manager
description: Flutter plugin for accessing the Android AlarmManager service, and
running Dart code in the background when alarms fire.
version: 0.4.2
version: 0.4.3
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/android_alarm_manager

Expand Down