oneShotAt static method

Future<bool> oneShotAt(
  1. DateTime time,
  2. int id,
  3. Function callback, {
  4. bool alarmClock = false,
  5. bool allowWhileIdle = false,
  6. bool exact = false,
  7. bool wakeup = false,
  8. bool rescheduleOnReboot = false,
})

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.

callback can be Function() or Function(int)

The 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 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.

Implementation

static Future<bool> oneShotAt(
  DateTime time,
  int id,
  Function callback, {
  bool alarmClock = false,
  bool allowWhileIdle = false,
  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 startMillis = time.millisecondsSinceEpoch;
  final CallbackHandle? handle = _getCallbackHandle(callback);
  if (handle == null) {
    return false;
  }
  final bool? r =
      await _channel.invokeMethod<bool>('Alarm.oneShotAt', <dynamic>[
    id,
    alarmClock,
    allowWhileIdle,
    exact,
    wakeup,
    startMillis,
    rescheduleOnReboot,
    handle.toRawHandle(),
  ]);
  return r ?? false;
}