better background tasks

This commit is contained in:
brausjonas
2024-09-22 15:10:47 +02:00
parent 54eb6814da
commit 7bd8ae8373
4 changed files with 47 additions and 21 deletions
+27 -20
View File
@@ -10,6 +10,7 @@ import 'package:disable_battery_optimization/disable_battery_optimization.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:math';
import 'package:workmanager/workmanager.dart';
String formatMinutes(int minutes) {
int hours = minutes ~/ 60;
@@ -29,6 +30,8 @@ String formatMinutes(int minutes) {
@pragma('vm:entry-point')
void callbackDispatcher() async {
WidgetsFlutterBinding.ensureInitialized();
Map content = await FileHandler.fileContent;
int id = 15;
for (String key in content.keys) {
@@ -62,26 +65,26 @@ void callbackDispatcher() async {
}
}
@pragma('vm:entry-point')
void onStart(ServiceInstance service) {
Timer.periodic(const Duration(minutes: 15), (timer) {
callbackDispatcher();
});
}
// @pragma('vm:entry-point')
// void onStart(ServiceInstance service) {
// Timer.periodic(const Duration(minutes: 15), (timer) {
// callbackDispatcher();
// });
// }
Future<void> initService() async {
final service = FlutterBackgroundService();
await service.configure(
iosConfiguration: IosConfiguration(),
androidConfiguration: AndroidConfiguration(
onStart: onStart,
isForegroundMode: false,
autoStart: true,
autoStartOnBoot: true));
service.startService();
}
// Future<void> initService() async {
// final service = FlutterBackgroundService();
//
// await service.configure(
// iosConfiguration: IosConfiguration(),
// androidConfiguration: AndroidConfiguration(
// onStart: onStart,
// isForegroundMode: false,
// autoStart: true,
// autoStartOnBoot: true));
//
// service.startService();
// }
void main() async {
WidgetsFlutterBinding.ensureInitialized();
@@ -95,7 +98,11 @@ void main() async {
)
]);
await initService();
Workmanager().initialize(callbackDispatcher);
Workmanager().registerPeriodicTask("1", "notifyTask", frequency: Duration(minutes: 15));
// await initService();
runApp(const MyApp());
}
+2 -1
View File
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todoapp/modals/addlistmodal.dart';
import 'package:todoapp/providers/todoprovider.dart';
import 'package:restart_app/restart_app.dart';
class TodoDrawer extends StatefulWidget {
const TodoDrawer({super.key});
@@ -44,7 +45,7 @@ class _TodoDrawerState extends State<TodoDrawer> {
],
),
)
)
),
],
),
),
+16
View File
@@ -304,6 +304,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.1.2"
restart_app:
dependency: "direct dev"
description:
name: restart_app
sha256: "00d5ec3e9de871cedbe552fc41e615b042b5ec654385e090e0983f6d02f655ed"
url: "https://pub.dev"
source: hosted
version: "1.3.2"
shared_preferences:
dependency: "direct dev"
description:
@@ -437,6 +445,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.0"
workmanager:
dependency: "direct dev"
description:
name: workmanager
sha256: ed13530cccd28c5c9959ad42d657cd0666274ca74c56dea0ca183ddd527d3a00
url: "https://pub.dev"
source: hosted
version: "0.5.2"
xdg_directories:
dependency: transitive
description:
+2
View File
@@ -46,6 +46,8 @@ dev_dependencies:
android_intent_plus:
disable_battery_optimization:
shared_preferences:
restart_app:
workmanager:
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is