zem*_*nkh 7 android alarmmanager dart flutter
一旦到达当前时间,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 作为提醒应用程序。
首先,您可以在回调函数中使用alarmId,只需使用形式参数而不是实际参数,并使用变量来获取特定数据。
// The callback for our alarm
static Future<void> callback(int alarmId) async {
print('Alarm fired! : ' + alarmId);
// 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)
此外,您应该使用 sqflite 数据库,它是一个关系型本地数据库,它将提醒信息存储在移动设备的本地磁盘中,将来您可以使用这个唯一的 AlarmId 来获取它(如果您将其插入表中)
https://pub.dev/packages/sqflite
本文档将帮助您使用 sqflite 执行基本的 CRUD 操作。
希望它能帮助你。
归档时间: |
|
查看次数: |
766 次 |
最近记录: |