Simple Android foreground service in Flutter using Kotlin for non-native developers

Ivan Burlakov
3 min readJul 17, 2021

--

Photo by Artur Shamsutdinov on Unsplash

During the development of my Flutter app, I found the need to keep some of the code running, even if user/system tries to unload it from memory. Here is the article of how to create a bare minimum foreground service, so you can understand and develop the concept.

Why I did not use some of the existing packages?
1. Maybe there are really good ones, but I did not feel trust, polishness and stability, from those that I’ve seen.
2. I felt like in my case it is not a huge and difficult task to acomplish and it does not require a whole dependency to achieve it.
3. It was just entertaining for me to try to acomplish it raw way, try a new language, etc.

And… I searched for articles, videos, questions and found the lack of simple and explanatory (at least simple and self-explanatory code) examples on how to achieve this. So after about 3 days of frustration and hitting the wall with my forehead (not really), I reverse engineered and combined several of those articles, videos, questions and came out with the following code, which I tried to make as lightweight as possible. Let’s begin!

Warning! At at the time of this writing I am not a Kotlin/Java developer. I have 0 native development experience (about 3 days of Kotlin experience to be exact). Following content may be shocking and disturbing for native developers. It may not have all needed exception safety. I just tried to show the simplest example possible. But any suggestions in the coments are more than welcome!

Like in all proper culinary recipes, I would like to begin with the ingredients (TL;DR), and then I will briefly explain them:

Add this two lines to <Your Project Folder>\android\app\src\main\AndroidManifest.xml file:

Add following two files to <Your Project Folder>\android\app\src\main\kotlin\com\example\<your project name> folder, but edit the first lines as I commented:

MainActivity.kt:

ExampleService.kt:

Next is the main.dart file to support this example. I just created a sample Flutter project and edited it:

So what is happening here? Basically, we can talk to Kotlin code from Dart using MethodChannel, which we define in MainActivity.kt. Once the method does the work we answer to our Dart code using return.success() and handle the result in Dart. In my example we define the needed methods to start and stop our Service.

What is the Service? In Android it is a part of app logic, some process that can be running in background, even if the app is closed. And due to our use of the foreground notification, Android should not stop it by itself, unless the user force stoppes the application. In my example we define the Service, that creates the NotificationChannelif we are running Android Oreo or later, launches an ongoing foreground notification and updates the text in it after 5 seconds since the start of the Service (you can stop the process earlier).

That is it! I hope his example is enough to explain the main concepts, so you can start from here, explore other concepts and implement the needed function in your app. Thank you for reading!

You can find the whole example project on GitHub here.
Only edited Gist files here.

--

--

Ivan Burlakov

Full-Stack Software Engineer student and musician from Ukraine. Currently I am into web and mobile development… And into blues. 😋🎸