Skip to content

Commit 72e2b5e

Browse files
committed
Merge branch 'service_docs' of https://github.com/inclement/python-for-android into inclement-service_docs
2 parents 8d88642 + 9ccca5e commit 72e2b5e

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

doc/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Contents
3131
commands
3232
recipes
3333
bootstraps
34+
services
3435
apis
3536
troubleshooting
3637
contribute

doc/source/services.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Services
2+
========
3+
4+
python-for-android supports the use of Android Services, background
5+
tasks running in separate processes, in this case each running their
6+
own Python interpreter instance. These are the closest Android
7+
equivalent to multiprocessing on e.g. desktop platforms, and it is not
8+
possible to use normal multiprocessing on Android. Services are also
9+
the only way to run code when your app is not currently opened by the user.
10+
11+
Services must be declared when python-for-android is run. Each one
12+
will have its own main.py file with the Python script to be run. You
13+
can communicate with the service process from your app using e.g. `osc
14+
<https://pypi.python.org/pypi/python-osc>`__ or (a heavier option)
15+
`twisted <https://twistedmatrix.com/trac/>`__.
16+
17+
Service creation
18+
----------------
19+
20+
There are two ways to have services included in your APK.
21+
22+
Service folder
23+
~~~~~~~~~~~~~~
24+
25+
This basic method works with both the new SDL2 and old Pygame
26+
bootstraps. It is recommended to use the second method (below) where
27+
possible.
28+
29+
Create a folder named ``service`` in your app directory, and add a
30+
file ``service/main.py``. This file should contain the Python code
31+
that you want the service to run.
32+
33+
To start the service, use the :code:`start_service` function from the
34+
:code:`android` module (included automatically with the Pygame
35+
bootstrap, you must add it to the requirements manually with SDL2 if
36+
you wish to use this method)::
37+
38+
import android
39+
android.start_service(title='service name',
40+
description='service description',
41+
arg='argument to service')
42+
43+
Arbitrary scripts
44+
~~~~~~~~~~~~~~~~~
45+
46+
.. note:: This service method is *not supported* by the Pygame bootstrap.
47+
48+
This method is recommended for non-trivial use of services as it is
49+
more flexible, supporting multiple services and a wider range of
50+
options.
51+
52+
To create the service, create a python script with your service code
53+
and add a :code:`--service=myservice:/path/to/myservice.py` argument
54+
when calling python-for-android. The ``myservice`` name before the
55+
colon is the name of the service class, via which you will interact
56+
with it later. You can add multiple
57+
:code:`--service` arguments to include multiple services, which you
58+
will later be able to stop and start from your app.
59+
60+
To run the services (i.e. starting them from within your main app
61+
code), you must use PyJNIus to interact with the java class
62+
python-for-android creates for each one, as follows::
63+
64+
from jnius import autoclass
65+
service = autoclass('your.package.name.ServiceMyservice')
66+
mActivity = autoclass('your.package.name.PythonActivity').mActivity
67+
argument = ''
68+
service.start(mActivity, argument)
69+
70+
Here, ``your.package.name`` refers to the package identifier of your
71+
APK as set by the ``--package`` argument to python-for-android, and
72+
the name of the service is ``ServiceYourservicename``, in which
73+
``Yourservicename`` is the identifier passed to the ``--service``
74+
argument with the first letter upper case. You must also pass the
75+
``argument`` parameter even if (as here) it is an empty string. If you
76+
do pass it, the service can make use of this argument.
77+
78+
Services support a range of options and interactions not yet
79+
documented here but all accessible via calling other methods of the
80+
``service`` reference.

0 commit comments

Comments
 (0)