I am trying to get a handle on the nearly entirely undocumented Android Alarm Manager Plus, and have a very simple app to press a button, set an alarm, and fire the alarm as follows:
import 'package:flutter/material.dart';
import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await AndroidAlarmManager.initialize();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.pink,
),
home: SetAlarmPage(),
);
}
}
class SetAlarmPage extends StatefulWidget {
const SetAlarmPage({Key? key}) : super(key: key);
@override
State<SetAlarmPage> createState() => _SetAlarmPageState();
}
class _SetAlarmPageState extends State<SetAlarmPage> {
String test = "Press Me!";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Set an Alarm")),
body: Center(
child: ElevatedButton(
child: Text(test),
onPressed: () {
print(test + " Button Pressed...");
setAlarm();
},
),
),
floatingActionButton:
FloatingActionButton(onPressed: null, child: Icon(Icons.add)),
);
}
void setAlarm() async {
print("setAlarm");
final int alarmID = 1;
await AndroidAlarmManager.oneShot(Duration(minutes: 1), alarmID, playAlarm);
}
void playAlarm() {
print("playAlarm");
setState(() {
test = "Pressed!";
});
}
}
I manage to get the alarm service started, but beyond that, nothing. I have tried initializing the AndroidAlarmManager object both in main and in setAlarm, tried moving around ensureInitialized, tried setting different durations in oneShot, tried changing the ID, and tried firing a more simple alarm function. No matter what I do, the alarm wont set or fire.
I'm pretty sure its something simple, but for a core function of android, there is no real documentation on how to use it to speak of.
Does anyone know what android alarm manager plus wants that I'm not providing, here?
from Android Alarm Manager Plus in Flutter never fires oneShot
No comments:
Post a Comment