Skip to content

Commit ee58dee

Browse files
committed
Fixes and clarifies services auto-restart
1) Fixes services auto-restart introduced in kivy#643: `onTaskRemoved()` stops the service gracefully using `stopSelf()` when it gets killed. That way `onDestroy()` gets triggered and service will restart under different process if auto-restart is enabled. 2) Documents process behavior and auto-restart usage: Emphasis that services are running in a dedicated processes, different from the app process, which is not the Android default behavior. Adds a code snippet.
1 parent 9e8bb37 commit ee58dee

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

doc/source/services.rst

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ possible to use normal multiprocessing on Android. Services are also
88
the only way to run code when your app is not currently opened by the user.
99

1010
Services must be declared when building your APK. Each one
11-
will have its own main.py file with the Python script to be run. You
12-
can communicate with the service process from your app using e.g. `osc
13-
<https://pypi.python.org/pypi/python-osc>`__ or (a heavier option)
11+
will have its own main.py file with the Python script to be run.
12+
Please note that python-for-android explicitly runs services as separated
13+
processes by having a colon ":" in the beginning of the name assigned to
14+
the ``android:process`` attribute of the ``AndroidManifest.xml`` file.
15+
This is not the default behavior, see `Android service documentation
16+
<https://developer.android.com/guide/topics/manifest/service-element>`__.
17+
You can communicate with the service process from your app using e.g.
18+
`osc <https://pypi.python.org/pypi/python-osc>`__ or (a heavier option)
1419
`twisted <https://twistedmatrix.com/trac/>`__.
1520

1621
Service creation
@@ -87,3 +92,14 @@ documented here but all accessible via calling other methods of the
8792
your service folder you must use e.g. ``import service.module``
8893
instead of ``import module``, if the service file is in the
8994
``service/`` folder.
95+
96+
Service auto-restart
97+
~~~~~~~~~~~~~~~~~~~~
98+
99+
It is possible to make services restart automatically when they exit by
100+
calling ``setAutoRestartService(True)`` on the service object.
101+
The call to this method should be done within the service code::
102+
103+
from jnius import autoclass
104+
PythonService = autoclass('org.kivy.android.PythonService')
105+
PythonService.mService.setAutoRestartService(True)

pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ public void onDestroy() {
130130
Process.killProcess(Process.myPid());
131131
}
132132

133+
/**
134+
* Stops the task gracefully when killed.
135+
* Calling stopSelf() will trigger a onDestroy() call from the system.
136+
*/
137+
@Override
138+
public void onTaskRemoved(Intent rootIntent) {
139+
super.onTaskRemoved(rootIntent);
140+
stopSelf();
141+
}
142+
133143
@Override
134144
public void run(){
135145
String app_root = getFilesDir().getAbsolutePath() + "/app";

0 commit comments

Comments
 (0)