stop android service from inside the service

143 views
Skip to first unread message

Thomas Schulz

unread,
Sep 11, 2016, 6:30:14 AM9/11/16
to Kivy users support
Hi,

I have followed the recipe to setup a background service under kivy on adroid from here:
https://kivy.org/planet/2014/01/building-a-background-application-on-android-with-kivy/

I would like to have an fail-safe stopping method for the service from inside the service.
So when the client dies unexpectedly the service would kill itself after a short time.

The android developers website says that the service should call the
stopself()-method (https://developer.android.com/guide/components/services.html).

But I have no idea how to get a reference (self) to the service,
because the service was started from my main program (client main.py) and not from inside the service itself.



 

Alexander Taylor

unread,
Sep 11, 2016, 11:34:11 AM9/11/16
to Kivy users support
This guide is for old-style services. python-for-android now supports some better mechanisms, described at http://python-for-android.readthedocs.io/en/latest/services/ (and these new style services include a stop method you can just call).

However, to get a reference to the service, I think you can use something like autoclass('org.kivy.android.PythonService').mService - the PythonService sets its mService attribute to the value of the service instance.

Thomas Schulz

unread,
Sep 11, 2016, 5:18:14 PM9/11/16
to Kivy users support
Thanks a lot, Alexander.
 
I had to change the class from "org.kivy.android.PythonService" to
'org.renpy.android.PythonService' Maybe that is because I use the kivy launcher!?

from jnius import autoclass
#service_class = autoclass('org.kivy.android.PythonService').mService
service = autoclass('org.renpy.android.PythonService').mService
service.stopSelf()

Alexander Taylor

unread,
Sep 11, 2016, 5:19:10 PM9/11/16
to kivy-...@googlegroups.com
Oh, yes, it's renpy under the old toolchain/pygame bootstrap. My mistake.
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Kivy users support" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/kivy-users/RzahVoWN_x4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> kivy-users+...@googlegroups.com
> <mailto:kivy-users+...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.


signature.asc
Reply all
Reply to author
Forward
0 new messages