Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 993f895

Browse files
Afsar-Pashabkonyi
authored andcommitted
[android_alarm_manager] Added oneShotAt method to schedule a alarm at a given time (#1947)
1 parent 95ba642 commit 993f895

4 files changed

Lines changed: 65 additions & 7 deletions

File tree

packages/android_alarm_manager/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.3
2+
3+
* Added `oneShotAt` method to run `callback` at a given DateTime `time`.
4+
15
## 0.4.2
26

37
* Added support for setting alarms which work when the phone is in doze mode.

packages/android_alarm_manager/android/src/main/java/io/flutter/plugins/androidalarmmanager/AndroidAlarmManagerPlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void registerWith(Registrar registrar) {
5050
// alarmManagerPluginChannel is the channel responsible for receiving the following messages
5151
// from the main Flutter app:
5252
// - "AlarmService.start"
53-
// - "Alarm.oneShot"
53+
// - "Alarm.oneShotAt"
5454
// - "Alarm.periodic"
5555
// - "Alarm.cancel"
5656
final MethodChannel alarmManagerPluginChannel =
@@ -125,7 +125,7 @@ public void onMethodCall(MethodCall call, Result result) {
125125
PeriodicRequest periodicRequest = PeriodicRequest.fromJson((JSONArray) arguments);
126126
AlarmService.setPeriodic(mContext, periodicRequest);
127127
result.success(true);
128-
} else if (method.equals("Alarm.oneShot")) {
128+
} else if (method.equals("Alarm.oneShotAt")) {
129129
// This message indicates that the Flutter app would like to schedule a one-time
130130
// task.
131131
OneShotRequest oneShotRequest = OneShotRequest.fromJson((JSONArray) arguments);

packages/android_alarm_manager/lib/android_alarm_manager.dart

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,74 @@ class AndroidAlarmManager {
113113
bool exact = false,
114114
bool wakeup = false,
115115
bool rescheduleOnReboot = false,
116+
}) =>
117+
oneShotAt(
118+
DateTime.now().add(delay),
119+
id,
120+
callback,
121+
alarmClock: alarmClock,
122+
allowWhileIdle: allowWhileIdle,
123+
exact: exact,
124+
wakeup: wakeup,
125+
rescheduleOnReboot: rescheduleOnReboot,
126+
);
127+
128+
/// Schedules a one-shot timer to run `callback` at `time`.
129+
///
130+
/// The `callback` will run whether or not the main application is running or
131+
/// in the foreground. It will run in the Isolate owned by the
132+
/// AndroidAlarmManager service.
133+
///
134+
/// `callback` must be either a top-level function or a static method from a
135+
/// class.
136+
///
137+
/// The timer is uniquely identified by `id`. Calling this function again
138+
/// again with the same `id` will cancel and replace the existing timer.
139+
///
140+
/// If `alarmClock` is passed as `true`, the timer will be created with
141+
/// Android's `AlarmManagerCompat.setAlarmClock`.
142+
///
143+
/// If `allowWhileIdle` is passed as `true`, the timer will be created with
144+
/// Android's `AlarmManagerCompat.setExactAndAllowWhileIdle` or
145+
/// `AlarmManagerCompat.setAndAllowWhileIdle`.
146+
///
147+
/// If `exact` is passed as `true`, the timer will be created with Android's
148+
/// `AlarmManagerCompat.setExact`. When `exact` is `false` (the default), the
149+
/// timer will be created with `AlarmManager.set`.
150+
///
151+
/// If `wakeup` is passed as `true`, the device will be woken up when the
152+
/// alarm fires. If `wakeup` is false (the default), the device will not be
153+
/// woken up to service the alarm.
154+
///
155+
/// If `rescheduleOnReboot` is passed as `true`, the alarm will be persisted
156+
/// across reboots. If `rescheduleOnReboot` is false (the default), the alarm
157+
/// will not be rescheduled after a reboot and will not be executed.
158+
///
159+
/// Returns a [Future] that resolves to `true` on success and `false` on
160+
/// failure.
161+
static Future<bool> oneShotAt(
162+
DateTime time,
163+
int id,
164+
dynamic Function() callback, {
165+
bool alarmClock = false,
166+
bool allowWhileIdle = false,
167+
bool exact = false,
168+
bool wakeup = false,
169+
bool rescheduleOnReboot = false,
116170
}) async {
117-
final int now = DateTime.now().millisecondsSinceEpoch;
118-
final int first = now + delay.inMilliseconds;
171+
final int startMillis = time.millisecondsSinceEpoch;
119172
final CallbackHandle handle = PluginUtilities.getCallbackHandle(callback);
120173
if (handle == null) {
121174
return false;
122175
}
123-
final bool r = await _channel.invokeMethod<bool>('Alarm.oneShot', <dynamic>[
176+
final bool r =
177+
await _channel.invokeMethod<bool>('Alarm.oneShotAt', <dynamic>[
124178
id,
125179
alarmClock,
126180
allowWhileIdle,
127181
exact,
128182
wakeup,
129-
first,
183+
startMillis,
130184
rescheduleOnReboot,
131185
handle.toRawHandle(),
132186
]);

packages/android_alarm_manager/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: android_alarm_manager
22
description: Flutter plugin for accessing the Android AlarmManager service, and
33
running Dart code in the background when alarms fire.
4-
version: 0.4.2
4+
version: 0.4.3
55
author: Flutter Team <flutter-dev@googlegroups.com>
66
homepage: https://github.com/flutter/plugins/tree/master/packages/android_alarm_manager
77

0 commit comments

Comments
 (0)