我正在开发一个应用程序,每次用户锁定手机时,我都必须暂停或停止Android警报管理器的回调功能。我不知道如何检测用户锁定他的手机。
我尝试使用应用程序生命周期管理,但当用户锁定和最小化应用程序时的结果是相同的。下面是代码。请看一看:
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。
检查我的代码,怎么做,在这个代码中我确实停止了AndroidAlarmManager和alaram
import 'dart:developer' as developer;
import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:flutter_ringtone_player/flutter_ringtone_player.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const AlarmManagerExampleApp());
}
class AlarmManagerExampleApp extends StatelessWidget {
const AlarmManagerExampleApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
useMaterial3: true,
colorSchemeSeed: const Color(0x9f4376f8),
),
home: const _AlarmHomePage(),
);
}
}
class _AlarmHomePage extends StatefulWidget {
const _AlarmHomePage({Key? key}) : super(key: key);
@override
_AlarmHomePageState createState() => _AlarmHomePageState();
}
class _AlarmHomePageState extends State<_AlarmHomePage> {
int alarmId = 1;
Future<void> scheduleAlarm() async {
await AndroidAlarmManager.oneShot(
const Duration(microseconds: 0),
alarmId,
callback,
rescheduleOnReboot: true,
exact: true,
wakeup: true,
);
}
Future<void> scheduleCancelAlarm() async {
await AndroidAlarmManager.oneShot(
const Duration(microseconds: 0),
alarmId,
stopcallback,
rescheduleOnReboot: true,
exact: true,
wakeup: true,
);
}
@pragma('vm:entry-point')
static Future<void> callback() async {
developer.log('Alarm fired!');
FlutterRingtonePlayer.playAlarm(
looping: true,
asAlarm: false,
volume: 1.0,
);
}
@pragma('vm:entry-point')
static Future<void> stopcallback() async {
developer.log('Alarm stop!');
AndroidAlarmManager.cancel(1);
FlutterRingtonePlayer.stop();
}
@override
Widget build(BuildContext context) {
final textStyle = Theme.of(context).textTheme.bodySmall;
return Scaffold(
appBar: AppBar(
title: const Text('Android alarm manager plus example'),
elevation: 4,
actions: [
IconButton(
onPressed: () {
scheduleCancelAlarm();
},
icon: const Icon(Icons.stop),
),
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Alarm fired times',
style: textStyle,
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () async {
await scheduleAlarm();
},
child: const Text('Schedule OneShot Alarm'),
),
],
),
),
);
}
}