一旦到达当前时间,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,
);
Run Code Online (Sandbox Code Playgroud)
该调用函数在下面。但它无法从其父函数获取传递的变量。
// 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);
}
Run Code Online (Sandbox Code Playgroud)
我的主要目的是使用 Android Alarm Manager …