Outils pour utilisateurs

Outils du site


kivy_android_service

Ceci est une ancienne révision du document !


Kivy: Android Service

Pourquoi utiliser un service android ?

Les applications Android se mettent en pause, lors du verrouillage de l'écran, ou de la réduction de la fenêtre de l'application.
Pour avoir un script qui tourne en arrière plan, il faut utiliser un service.

Ressources

Extrait: python-for-android supports the use of Android Services, background tasks running in separate processes. These are the closest Android equivalent to multiprocessing on e.g. desktop platforms, and it is not possible to use normal multiprocessing on Android. Services are also the only way to run code when your app is not currently opened by the user.

Services must be declared when building your APK. Each one will have its own main.py file with the Python script to be run. Please note that python-for-android explicitly runs services as separated processes by having a colon “:” in the beginning of the name assigned to the android:process attribute of the AndroidManifest.xml file. This is not the default behavior, see Android service documentation. You can communicate with the service process from your app using e.g. osc or (a heavier option) twisted.

Exemple d'un développeur kivy

Comme l'exemple utilise osc, je vais faire une infidélité à twisted. Je veux un truc qui marche vite, tant pis pour l'élégance.

Cet exemple utilise un Tread pour tester le code sur les autres platformes que Android. Sauf que ce Thread est stoppé avec un Tread.stop(), méthode qui n'existe pas: l'application plante! Il faudait communiquer avec le service en envoyant un message osc “stop” qui finirait la boucle while.
La seule façon de terminer un thread est de basculer à 0 un self.loop de:

while self.loop:
    pass
kivy_android_service.1604305625.txt.gz · Dernière modification : 2020/11/02 08:27 de serge