6

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

How to keep kivy service running in background in Android (service still run when switch to other App or lock the screen)?

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.

Miguel
  • 61
  • 3
  • I'm trying (again) the example in https://github.com/kivy/kivy/wiki/Background-Service-using-P4A-android.service but in this line `service = jnius.autoclass('org.kivy.android.PythonService').mService` it returns None, so the rest of the orders that use `service` raise an error. `org.kivy.android.PythonService` is a Java file that (among others) assign `None` to `mService` and only when the service is running assigns it a value. Does anybody know how to solve this? – Miguel Oct 22 '20 at 18:39
  • Have you found the solution? If yes then can you put your files here. I'm also stuck with this – Ankit Sangwan Jan 02 '21 at 04:47

0 Answers0