【发布时间】:2021-07-02 16:26:53
【问题描述】:
如果我的用户设置了一些数据,例如:
day : "Sunday"
startTime: 8:00 A.M
endTime: 8:00 P.M
frequency: 30 minutes
我想从8:00 A.M 开始,每 30 分钟触发一次警报,所以,8:00 A.M,8:30 A.M,9:00 A.M,9:30 A.M ....
现在我在我的应用程序中使用android_alarm_manager_plus,这就是我到目前为止所做的:
AndroidAlarmManager.periodic(
const Duration(minutes: 0, seconds: 1),
0,
printHello, // callback function, for now I'm just printing "hello world"
);
如何在用户选择的日期、时间和频率上设置闹钟?
更新 1:
AndroidAlarmManager.periodic(
const Duration(minutes: _frequency!), //Evaluation of this constant expression throws an exception.
0,
printHello,
startAt: DateTime(
DateTime.now().year,
DateTime.now().month,
DateTime.now().day,
_startTime, //The argument type 'TimeOfDay?' can't be assigned to the parameter type 'int'
0,
),
);
我如何存储数据:
int? frequency;
TimeOfDay? startTime;
我的 TimeOfDay 选择器:
void selectStartTime() async {
final TimeOfDay? newTime = await showTimePicker(
context: context,
initialTime: _startTime!,
initialEntryMode: TimePickerEntryMode.input,
);
if (newTime != null) {
setState(() {
_startTime = newTime;
});
}
}
更新 2: 好的,所以我检查了 android alarm manager plus 的源代码,我认为它们不支持我正在尝试做的开箱即用。
这是他们的周期性计时器的代码:
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 now = _now().millisecondsSinceEpoch;
final period = duration.inMilliseconds;
final first =
startAt != null ? startAt.millisecondsSinceEpoch : now + period;
final handle = _getCallbackHandle(callback);
if (handle == null) {
return false;
}
final r = await _channel.invokeMethod<bool>('Alarm.periodic', <dynamic>[
id,
exact,
wakeup,
first,
period,
rescheduleOnReboot,
handle.toRawHandle()
]);
return (r == null) ? false : r;
}
是否可以创建另一个自定义函数来执行我想要的操作?自定义函数看起来像这样:
static Future<bool> customPeriodic(
int id, // id
Duration repeatAfter, // repeats after each m time(ex: 7 days)
int frequency, // fire alarm after each n minutes(ex: 30 mins)
Function callBack, {
DateTime? startAt, // serve as start tune
DateTime? endAt, // serve as end time
bool exact = false,
bool wakeup = false,
bool rescheduleOnReboot = false,
}) async {
return true;
}
【问题讨论】:
标签: flutter dart android-alarms