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