Skip to content

Instantly share code, notes, and snippets.

@Andrious
Last active July 19, 2020 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andrious/aebfb5ce5f8fe3cce00fb51444732b3d to your computer and use it in GitHub Desktop.
Save Andrious/aebfb5ce5f8fe3cce00fb51444732b3d to your computer and use it in GitHub Desktop.
Example code used to demonstrate the use of the library file, alarm_manager.dart, working with the plugin, android_alarm_manager.
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// ignore_for_file: public_member_api_docs
import 'dart:math' show Random, pow;
import 'package:flutter/material.dart';
import 'alarm_manager.dart' show AlarmManager;
import 'package:shared_preferences/shared_preferences.dart'
show SharedPreferences;
/// The [SharedPreferences] key to access the alarm fire count.
const String countKey = 'count';
/// Global [SharedPreferences] object.
SharedPreferences prefs;
void main() => runApp(AlarmManagerExampleApp());
class AlarmManagerExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: FutureBuilder<bool>(
future: initSettings(),
initialData: false,
builder: (_, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return _AlarmHomePage(title: 'Flutter Demo Home Page');
} else {
return Container(
child: Center(child: CircularProgressIndicator()));
}
}));
}
Future<bool> initSettings() async {
bool init = await AlarmManager.init(
exact: true,
alarmClock: true,
wakeup: true,
);
prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey(countKey)) prefs.setInt(countKey, 0);
return init;
}
}
class _AlarmHomePage extends StatefulWidget {
_AlarmHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_AlarmHomePageState createState() => _AlarmHomePageState();
}
class _AlarmHomePageState extends State<_AlarmHomePage> {
@override
Widget build(BuildContext context) {
final textStyle = Theme.of(context).textTheme.headline4;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Alarm fired $_counter times',
style: textStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Total alarms fired: ',
style: textStyle,
),
Text(
prefs.getInt(countKey).toString(),
key: ValueKey('BackgroundCountText'),
style: textStyle,
),
],
),
RaisedButton(
child: Text(
'Schedule OneShot Alarm',
),
key: ValueKey('RegisterOneShotAlarm'),
onPressed: () async {
await AlarmManager.oneShot(
const Duration(seconds: 5),
// Ensure we have a unique alarm ID.
Random().nextInt(pow(2, 31)),
(int id) => _incrementCounter(),
);
},
),
],
),
),
);
}
int _counter = 0;
Future<void> _incrementCounter() async {
int currentCount = prefs.getInt(countKey);
await prefs.setInt(countKey, currentCount + 1);
// Ensure we've loaded the updated count from the background isolate.
await prefs.reload();
setState(() {
_counter++;
});
print('Increment counter!');
}
}
@Kinasr
Copy link

Kinasr commented Jul 19, 2020

not working when kill app

@Andrious
Copy link
Author

Hello there, @Kinasr. If you like, you can detail the problem and post an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment