Kivy: Stop a android service from main App

1.1k Views Asked by At

If the following is how you start an android service with Kivy, how do you stop a service from the front end? I already know how to make the service stop itself in the service, i want to stop it from the frontend:

from android import AndroidService
service = AndroidService('myApp', 'status: active')
service.start('service started')
self.service = service

I tried the following but it does not work:

from jnius import autoclass
service = autoclass('org.renpy.android.PythonService').mService
service.stopService()
1

There are 1 best solutions below

2
On

If you use python-for-android's newer services api, which I recommend, you can manage the service via pyjnius as follows:

        from jnius import autoclass
        service = autoclass('your.service.name.ClassName')
        mActivity = autoclass('org.kivy.android.PythonActivity').mActivity
        service.stop(mActivity)

If using the old way with the android module (which isn't that well supported right now), it looks like you should be able to call service.stop() on your AndroidService object.