periodic static method
Schedules a repeating timer to run callback
with period duration
.
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.
callback
can be Function()
or Function(int)
The repeating timer is uniquely identified by id
. Calling this function
again with the same id
will cancel and replace the existing timer.
id
will passed to callback
if it is of type Function(int)
If startAt
is passed, the timer will first go off at that time and
subsequently run with period duration
.
If exact
is passed as true
, the timer will be created with Android's
AlarmManager.setRepeating
. When exact
is false
(the default), the
timer will be created with AlarmManager.setInexactRepeating
.
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.
Implementation
static Future<bool> periodic(
Duration duration,
int id,
Function callback, {
DateTime? startAt,
bool exact = false,
bool wakeup = false,
bool rescheduleOnReboot = false,
}) async {
// ignore: inference_failure_on_function_return_type
assert(callback is Function() || callback is Function(int));
assert(id.bitLength < 32);
final int now = _now().millisecondsSinceEpoch;
final int period = duration.inMilliseconds;
final int first =
startAt != null ? startAt.millisecondsSinceEpoch : now + period;
final CallbackHandle? handle = _getCallbackHandle(callback);
if (handle == null) {
return false;
}
final bool? r = await _channel.invokeMethod<bool>(
'Alarm.periodic', <dynamic>[
id,
exact,
wakeup,
first,
period,
rescheduleOnReboot,
handle.toRawHandle()
]);
return r ?? false;
}