|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | | -import 'dart:async'; |
6 | 5 | import 'dart:isolate'; |
| 6 | +import 'dart:ui'; |
7 | 7 |
|
| 8 | +import 'package:android_alarm_manager_example/alarms.dart'; |
| 9 | +import 'package:flutter/material.dart'; |
8 | 10 | import 'package:android_alarm_manager/android_alarm_manager.dart'; |
9 | | -import 'package:firebase_auth/firebase_auth.dart'; |
10 | | -import 'package:flutter/widgets.dart'; |
11 | 11 |
|
12 | | -final FirebaseAuth firebaseAuth = FirebaseAuth.instance; |
13 | | -FirebaseUser firebaseUser; |
| 12 | +void main() async { |
| 13 | + // Start the AlarmManager service. |
| 14 | + await AndroidAlarmManager.initialize(); |
14 | 15 |
|
15 | | -Future<void> ensureFirebaseUser() async { |
16 | | - if (firebaseUser == null) { |
17 | | - firebaseUser = await firebaseAuth.currentUser(); |
18 | | - if (firebaseUser == null) { |
19 | | - firebaseUser = await firebaseAuth.signInAnonymously(); |
20 | | - } |
21 | | - } |
| 16 | + printLocalMessage("AndroidAlarmManager initialized!"); |
| 17 | + runApp(MyApp()); |
22 | 18 | } |
23 | 19 |
|
24 | | -class HelloMessage { |
25 | | - HelloMessage(this._now, this._msg, this._isolate, this._user, this._token); |
26 | | - |
27 | | - final DateTime _now; |
28 | | - final String _msg; |
29 | | - final int _isolate; |
30 | | - final FirebaseUser _user; |
31 | | - final String _token; |
32 | | - |
| 20 | +class MyApp extends StatelessWidget { |
33 | 21 | @override |
34 | | - String toString() { |
35 | | - return "[$_now] $_msg " |
36 | | - "isolate=$_isolate " |
37 | | - "user='$_user' " |
38 | | - "token=$_token"; |
| 22 | + Widget build(BuildContext context) { |
| 23 | + return MaterialApp( |
| 24 | + title: 'Alarm Manager Demo', |
| 25 | + theme: ThemeData( |
| 26 | + primarySwatch: Colors.blue, |
| 27 | + ), |
| 28 | + home: MyHomePage(title: 'Simple Alarm Manager Demo'), |
| 29 | + ); |
39 | 30 | } |
40 | 31 | } |
41 | 32 |
|
42 | | -void printHelloMessage(String msg) { |
43 | | - ensureFirebaseUser().then((_) { |
44 | | - firebaseUser.getIdToken().then((String idToken) { |
45 | | - print(HelloMessage( |
46 | | - DateTime.now(), |
47 | | - msg, |
48 | | - Isolate.current.hashCode, |
49 | | - firebaseUser, |
50 | | - idToken, |
51 | | - )); |
52 | | - }); |
53 | | - }); |
54 | | -} |
| 33 | +class MyHomePage extends StatefulWidget { |
| 34 | + MyHomePage({Key key, this.title}) : super(key: key); |
55 | 35 |
|
56 | | -void printHello() { |
57 | | - printHelloMessage("Hello, world!"); |
58 | | -} |
| 36 | + final String title; |
59 | 37 |
|
60 | | -void printGoodbye() { |
61 | | - printHelloMessage("Goodbye, world!"); |
| 38 | + @override |
| 39 | + _MyHomePageState createState() => _MyHomePageState(); |
62 | 40 | } |
63 | 41 |
|
64 | | -bool oneShotFired = false; |
| 42 | +class _MyHomePageState extends State<MyHomePage> { |
| 43 | + bool _startedAlarm = false; |
| 44 | + int _periodic = 0; |
| 45 | + ReceivePort _foregroundPort = ReceivePort(); |
65 | 46 |
|
66 | | -void printOneShot() { |
67 | | - printHelloMessage("Hello, once!"); |
68 | | -} |
| 47 | + @override |
| 48 | + void initState() { |
| 49 | + super.initState(); |
| 50 | + initPlatformState(); |
| 51 | + } |
69 | 52 |
|
70 | | -Future<void> main() async { |
71 | | - final int helloAlarmID = 0; |
72 | | - final int goodbyeAlarmID = 1; |
73 | | - final int oneShotID = 2; |
| 53 | + void initPlatformState() { |
| 54 | + // The IsolateNameServer allows for us to create a mapping between a String |
| 55 | + // and a SendPort that is managed by the Flutter engine. A SendPort can |
| 56 | + // then be looked up elsewhere, like a background callback, to establish |
| 57 | + // communication channels between isolates that were not spawned by one |
| 58 | + // another. |
| 59 | + if (!IsolateNameServer.registerPortWithName( |
| 60 | + _foregroundPort.sendPort, kAlarmManagerExamplePortName)) { |
| 61 | + throw 'Unable to register port!'; |
| 62 | + } |
| 63 | + _foregroundPort.listen((dynamic message) { |
| 64 | + final int periodicCount = message; |
| 65 | + print('periodicCount was: $periodicCount'); |
| 66 | + setState(() { |
| 67 | + _periodic = periodicCount; |
| 68 | + }); |
| 69 | + }, onDone: () {}); |
| 70 | + } |
74 | 71 |
|
75 | | - // Start the AlarmManager service. |
76 | | - await AndroidAlarmManager.initialize(); |
| 72 | + @override |
| 73 | + void dispose() { |
| 74 | + super.dispose(); |
| 75 | + // Remove the port mapping just in case the UI is shutting down but |
| 76 | + // background isolate is continuing to run. |
| 77 | + IsolateNameServer.removePortNameMapping(kAlarmManagerExamplePortName); |
| 78 | + } |
77 | 79 |
|
78 | | - printHelloMessage("Hello, main()!"); |
79 | | - runApp(const Center( |
80 | | - child: Text('Hello, world!', textDirection: TextDirection.ltr))); |
81 | | - await AndroidAlarmManager.periodic( |
82 | | - const Duration(seconds: 5), helloAlarmID, printHello, |
83 | | - wakeup: true); |
84 | | - await AndroidAlarmManager.oneShot( |
85 | | - const Duration(seconds: 5), goodbyeAlarmID, printGoodbye); |
86 | | - if (!oneShotFired) { |
87 | | - await AndroidAlarmManager.oneShot( |
88 | | - const Duration(seconds: 5), oneShotID, printOneShot); |
| 80 | + void _startAlarms() { |
| 81 | + startAlarms(); |
| 82 | + setState(() { |
| 83 | + _startedAlarm = true; |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + void _stopAlarms() { |
| 88 | + stopAlarms(); |
| 89 | + setState(() { |
| 90 | + _startedAlarm = false; |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @override |
| 95 | + Widget build(BuildContext context) { |
| 96 | + return Scaffold( |
| 97 | + appBar: AppBar( |
| 98 | + title: Text(widget.title), |
| 99 | + ), |
| 100 | + body: Center( |
| 101 | + child: Column( |
| 102 | + mainAxisAlignment: MainAxisAlignment.center, |
| 103 | + children: <Widget>[ |
| 104 | + const Text('Alarms are:'), |
| 105 | + Text( |
| 106 | + _startedAlarm ? 'running ($_periodic)' : 'stopped ($_periodic)', |
| 107 | + style: Theme.of(context).textTheme.display1, |
| 108 | + ), |
| 109 | + Row( |
| 110 | + mainAxisAlignment: MainAxisAlignment.center, |
| 111 | + children: <Widget>[ |
| 112 | + RaisedButton( |
| 113 | + onPressed: _startedAlarm ? null : _startAlarms, |
| 114 | + child: const Text('start'), |
| 115 | + ), |
| 116 | + Container(padding: const EdgeInsets.all(20.0)), |
| 117 | + RaisedButton( |
| 118 | + onPressed: _startedAlarm ? _stopAlarms : null, |
| 119 | + child: const Text('stop'), |
| 120 | + ), |
| 121 | + ], |
| 122 | + ), |
| 123 | + ], |
| 124 | + ), |
| 125 | + ), |
| 126 | + ); |
89 | 127 | } |
90 | 128 | } |
0 commit comments