Reputation:
I'm having a hard time trying to implement a background task in flutter. I tried the android_alarm_manager plugin, but as the docs specify the alarm manager is inexact:
Note: Beginning with API 19 (Build.VERSION_CODES.KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, android.app.PendingIntent) and setExact(int, long, android.app.PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.
So a timer that should run periodically every minute, sometimes missed a minute:
To
implement a process that watches the time, or that catches camera movement.
the flutter docs (and other sources) link to a medium article from 2018 where the author shares his experience developing plugins which take advantage of background execution of Dart code. But I have trouble to understand this article and also it seems overwhelming for simple tasks...
I also read about the possibility to use MethodCannel to write platform specific code, but I have never worked with android before and have therefore no clue how to do it.
Is there a simpler way to create a background task in flutter? E.g. imagine a very simple app where the only purpose is to run in the background and check the time. If a quarter of an hour is achieved (e.g 1:15 pm) the app should make a sound. Every half a hour the app should make another sound (e.g. 1:30 pm) and every full hour another another sound (e.g 2 pm).
Doesn't seem like that big kind of a deal, but I don't really know how to do it. Have you some ideas?
Upvotes: 2
Views: 7830
Reputation: 1651
The problem with you want to do is that both Android and IOS don't let you run background processes continuously. IOS is even more restrictive about it than Android.
For IOS your app is only allowed to keep running in the background in very specific cases. For example, these include playing audio, getting location updates or fetching the latest content from a server. And the minimum interval between two executions is 15 minutes.
The way to overcome this problem is using some sort of scheduling system to either run a simple process or to allow the user to open the app.
There are some usefull packages to help you with this:
If you need to run background processes continuously, take a look at this package:
https://pub.dev/packages/background_fetch
If you need to show scheduled messages, use local notifications:
https://pub.dev/packages/flutter_local_notifications
Another usefull lib to run background process:
https://pub.dev/packages/workmanager
But note that none of these packages will do exactly what you want. Maybe you should think about alternative ways to solve the problem. I have faced a similar problem building alarms and timers. If you like, you could share more about your needs and I'll be glad to help.
Upvotes: 1