Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: cross-platform way to keep application running in the background

Tags:

flutter

I'm trying to figure out a way to keep a Flutter application running even if it's not in focus. For instance have a countdown running and play an alarm sound/show notification on completion, no matter what's running in foreground. Can anyone point me in the right direction? Ideally something that works cross-platform.

I found this thread but it's almost 2 years old, so I'm thinking maybe there has been some development since then.

like image 269
dan Avatar asked Nov 23 '18 16:11

dan


People also ask

Does Flutter timer run in background?

A periodic Timer which runs only while the app's lifecycle is resumed. The default Dart timer also runs while the app is paused, e.g. if it is in the background.

How do I run code in the background even with the screen off?

You can use the android_alarm_manager flutter plugin which lets you run Dart code in the background when an alarm fires. Another way with more control would be to write a native Android service (using Java or Kotlin) for your app that communicates with the flutter frontend via device storage or shared prefs.


1 Answers

This looks like what you might want: https://medium.com/flutter-io/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124. It's a nice article on Medium describing how to run things in the background on both iOS and Android, especially using the most recent releases of Flutter.

The official documentation for Flutter in the background is at https://flutter.io/docs/development/packages-and-plugins/background-processes. The key is to run your code in an Isolate, because you won't have access to the GUI Isolate when you are in the background.

like image 137
Randal Schwartz Avatar answered Nov 09 '22 08:11

Randal Schwartz