Last active
July 19, 2020 16:30
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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!'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not working when kill app