Android闹钟管理器一旦到达设置的时间就会触发,同时也会触发回调函数来处理一些任务。但是,在向回调函数发送消息或参数以执行某些任务时,如何在闹钟管理器中设置特定值来设置特定闹钟并触发回调函数还不清晰。
await AndroidAlarmManager.oneShot(
const Duration(seconds: 5),
// Ensure we have a unique alarm ID.
Random().nextInt(pow(2, 31)),
callback,
exact: true,
wakeup: true,
);
那个调用函数在这里。但它无法获取来自其父函数的变量。
// The callback for our alarm
static Future<void> callback() async {
print('Alarm fired!');
// Get the previous cached count and increment it.
final prefs = await SharedPreferences.getInstance();
int currentCount = prefs.getInt(countKey);
await prefs.setInt(countKey, currentCount + 1);
// This will be null if we're running in the background.
uiSendPort ??= IsolateNameServer.lookupPortByName(isolateName);
uiSendPort?.send(null);
}
我的主要目的是将Android Alarm Manager用作提醒应用程序。