gpt4 book ai didi

flutter - 如何在Flutter中暂停或停止Android Alarm Manager的回调功能?

转载 作者:行者123 更新时间:2023-12-03 03:54:54 27 4
gpt4 key购买 nike

我正在开发一个应用程序,每次用户锁定手机时,我都必须暂停或停止Android Alarm Manager的回调功能。我也不知道如何检测用户是否锁定了手机。

我尝试使用应用程序生命周期管理,但是当用户同时锁定和最小化应用程序时的结果是相同的。下面是代码。请看一下:

import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:intl/intl.dart';

import 'CommonPicker.dart';
import 'fileUtils.dart';
import 'package:android_alarm_manager/android_alarm_manager.dart';


void callback(){
print("I am in the isolate");
DateTime now=DateTime.now().toLocal();
print("Date time:");
print(now);
//operation to be performed
print("last line of Isolate");
}

class AlertTime2 extends StatefulWidget {
static const routeName = '/alertTime2';

@override
_AlertTime2State createState() => _AlertTime2State();
}

class _AlertTime2State extends State<AlertTime2> {
String mm = "00";
String ss = "00";
var mmValue;
var ssValue;
var totalTimeInSec;
String _selectedTime;

@override
void initState() {
super.initState();

FileUtils().readFromFile().then((String value) {
setState(() {
_selectedTime = value;
});
});
}


void alarm_managing_function(int t) async {
await AndroidAlarmManager.initialize();
await AndroidAlarmManager.periodic(Duration(seconds: t), 0, callback);
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(30, 70, 0, 0),
child: Text(
"Timer for Notification",
style: TextStyle(
fontSize: 50,
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 150, 0, 0),
child: Text(
"MM-SS",
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
child: Text(
"${_selectedTime ?? 'Selected Time'}",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 38),
),
),
RaisedButton(
onPressed: () {
DatePicker.showPicker(context,
showTitleActions: true,
pickerModel:
CustomPicker(currentTime: DateTime.tryParse(_selectedTime)),
onConfirm: (time) {
setState(() {
print(time);
_selectedTime = DateFormat("mm-ss").format(time);
FileUtils().saveToFile(_selectedTime);

mm = _selectedTime.substring(0, 2);
ss = _selectedTime.substring(3, 5);
print(mm);
print(ss);
mmValue = int.parse(mm);
ssValue = int.parse(ss);
totalTimeInSec = mmValue * 60 + ssValue;
print(totalTimeInSec);
DateTime now=DateTime.now().toLocal();
alarm_managing_function(totalTimeInSec);
});
}, locale: LocaleType.en);
},
child: Text("Show Time picker"),
),

),
],
));
}
}

最佳答案

您已插入此代码行以停止或暂停警报

cancel = await AndroidAlarmManager.cancel(id);
传递您要停止的警报的ID。

关于flutter - 如何在Flutter中暂停或停止Android Alarm Manager的回调功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61730748/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com