How to make a simple Flutter stopwatch foreground service on Android?

650

You can read about foreground service here https://developer.android.com/guide/components/foreground-services You should implement your counter in the service because dart doesn't work in background. And call start/stop from dart through platform channels.

Share:
650
John
Author by

John

Updated on December 31, 2022

Comments

  • John
    John over 1 year

    I am implementing a stopwatch (0 to .. seconds counter) in my app. I succesfully implemented it in my Dart code, but I need it to work even if the app is closed/unloaded from memory, etc. Therefore I need a foreground service with an ongoing notification and a counter in it.

    I have read and watched about every question/article/video on the internet about integrating an Android foreground service in a Flutter app. And still after several days I did not find a simple and explanatory example on how to do it. I followed this official tutorial and succeeded in connecting Flutter side to Kotlin code https://flutter.dev/docs/development/platform-integration/platform-channels . But I still do not understand how do I launch a foreground service. And where is it better to implement my counter, in the Kotlin service or in dart? Would appreciate any help.