Skip to content Skip to sidebar Skip to footer

How To Keep Kivy Service Running In Background In Android (service Still Run When Switch To Other App Or Lock The Screen)?

I'm trying to build an android APP with kivy,my requirement is quit simple: When open the android APP,it will start counting from 0, and a prompt will pop up in the status bar ever

Solution 1:

After days of testing, I solved the problem

My previous understanding of background operation is indeed problematic, or my solution is just one of the ways to achieve the goal.

in main.py, define the service and start it:

from jnius import autoclassSERVICE_NAME = u'{packagename}.Service{servicename}'.format(
    packagename=u'org.kivy.test',
    servicename=u'Myservice'
)
service = autoclass(SERVICE_NAME)
mActivity = autoclass(u'org.kivy.android.PythonActivity').mActivity
argument = ''
service.start(mActivity, argument)

in buildozer.spec:

# (str) Package namepackage.name = test

# (str) Package domain (needed for android/ios packaging)package.domain = org.kivy

# (list) List of service to declareservices = Myservice:service.py

and then, edit the service.py as needed. The main.py and service.py can communicate using oscpy.

By doing the above, the notification can be popped up after open APP, even the APP is switched to the background.

Solution 2:

is this code

from jnius import autoclassservice = autoclass(SERVICE_NAME)
mActivity = autoclass(u'org.kivy.android.PythonActivity').mActivity
argument = ''
service.start(mActivity, argument)

inside the service.py or main.py

Post a Comment for "How To Keep Kivy Service Running In Background In Android (service Still Run When Switch To Other App Or Lock The Screen)?"