在用户选择的工作日颤动重复警报
Flutter repeat alarm on user selected week days
如果我的用户设置了一些数据,例如:
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;
我的时间选择器:
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;
}
添加startAt参数。在这段代码中,它将在当天早上 8 点开始。您可以随时更改为开始
AndroidAlarmManager.periodic(
const Duration(minutes: 30), 0,
printHello,
startAt: DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day, 8, 0),
);
如果我的用户设置了一些数据,例如:
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;
我的时间选择器:
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;
}
添加startAt参数。在这段代码中,它将在当天早上 8 点开始。您可以随时更改为开始
AndroidAlarmManager.periodic(
const Duration(minutes: 30), 0,
printHello,
startAt: DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day, 8, 0),
);