Background Service using P4A android.service

FeralBytes edited this page Feb 17, 2015 · 1 revision
Clone this wiki locally

Create your service as normal in the folder service/ as main.py. When you want your service to no longer run in the foreground, but rather to be a background service just execute this code.

from jnius import autoclass
Service = autoclass('org.renpy.android.PythonService').mService
Service.stopForeground(True)

Just remember this service is removed from foreground state, allowing it to be killed if more memory is needed.

Here is the Java Documentation on stopForeground()

You still start your service as normal from your app.

service = android.AndroidService('Notification Name', 'Notification Message')
service.start('Service args')

Reference: https://github.com/kivy/python-for-android/issues/267