我正在尝试创建一个可以在关闭应用程序时运行的秒表应用程序。我尝试了这段代码,但是当应用程序关闭(正常)时,循环停止了。有什么办法可以实现这个目标。平台不允许每秒运行后台服务。
int counter = 0;
Future<void> _showProgressNotification() async {
var maxProgress = 25;
counter = 0;
for (var i = 0; i <= maxProgress; i++) {
counter++;
await Future.delayed(Duration(seconds: 1), () async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'progress channel',
'progress channel',
'progress channel description',
channelShowBadge: false,
importance: Importance.Max,
priority: Priority.High,
onlyAlertOnce: true,
showProgress: false,
maxProgress: maxProgress,
progress: i);
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
"Kronometre - Anatomi",
"Süre: $counter",
platformChannelSpecifics,
payload: 'item x');
});
}
}