I'm trying to execute a Foreground Service in Android using kivy.
I have created a service following different examples in internet, but all of them are killed by Android after some minutes after going to sleep (Doze mode) or even when I change to another application.
I have tried several examples and try different options but no one stays alive after some minutes.
Among others I have tried
https://python-for-android.readthedocs.io/en/latest/services/
https://github.com/tshirtman/kivy_service_osc
https://github.com/kivy/kivy/wiki/Background-Service-using-P4A-android.service
It seems that since Android Oreo services spawned by an app will be killed as soon as the app is killed. And if you want to run a service in the background, you need to display an ongoing notification to the user telling them that your app is running in the background. But I do not know how to do that in kivy.
I would apreciate if someone can help me or show an example on how to do it.
Thanks in advance
EDIT: My solution at the moment, just in case it can help someone.
Finally I have been able to start the foreground service (so it wasn't killed by Android when phone goes to sleep or when you are using another app) with kivy but I have had to edit one file of buildozer structure.
I have used the example in https://github.com/tshirtman/kivy_service_osc to create the apk I have execute
buildozer -v android debug deploy run logcat
then I have edited .buildozer/android/platform/build-armeabi-v7a/dists/oscservice__armeabi-v7a/templates/Service.tmpl.java
(the service script is in service.y
file) and I have changed
intent.putExtra("serviceStartAsForeground", "{{ foreground|lower }}");
by
intent.putExtra("serviceStartAsForeground", "true");
and then I have executed again
buildozer -v android debug deploy run logcat
I have not found any other way to tell kivy that I want to execute a foreground service. I will appreciate if some knows and shares other better form to do it.