kivy_android_service
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
kivy_android_service [2020/10/30 10:35] – [Ressources] serge | kivy_android_service [2020/11/02 12:53] (Version actuelle) – [Kivy: Android Service] serge | ||
---|---|---|---|
Ligne 2: | Ligne 2: | ||
<WRAP center round box 60% centeralign> | <WRAP center round box 60% centeralign> | ||
**{{tagpage> | **{{tagpage> | ||
- | **[[http:// | + | **[[http:// |
</ | </ | ||
<WRAP center round box 40% centeralign> | <WRAP center round box 40% centeralign> | ||
Ligne 14: | Ligne 14: | ||
===== Ressources ===== | ===== Ressources ===== | ||
* https:// | * https:// | ||
+ | * https:// | ||
Extrait: | 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. | 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: | + | 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: |
+ | |||
+ | ====Exemple d'un développeur kivy==== | ||
+ | * **https:// | ||
+ | Comme l' | ||
+ | |||
+ | 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(), | ||
+ | **La seule façon de terminer un thread est de basculer à 0 un self.loop de:** | ||
+ | <code python> | ||
+ | while self.loop: | ||
+ | pass | ||
+ | </ | ||
+ | |||
+ | =====Kivy Accelerometer Service OSC===== | ||
+ | |||
+ | ====Sources sur GitHub==== | ||
+ | * **[[https:// | ||
+ | ===*.apk=== | ||
+ | Dans le dossier bin | ||
+ | |||
+ | ===TODO=== | ||
+ | Si il n'y a pas d' | ||
+ | |||
+ | ====Extrait du code pour le service==== | ||
+ | La communication entre main.py et service.py se fait en OSC, c'est très rapide, il n'y a aucune latence. | ||
+ | Il n'est pas possible de lancer service.py avec des arguments, ni d' | ||
+ | |||
+ | Pour l'OSC, voir [[kivy_oscpy|Kivy: | ||
+ | |||
+ | === buildozer.spec === | ||
+ | [[https:// | ||
+ | |||
+ | <code python> | ||
+ | [app] | ||
+ | title = Accelerometer | ||
+ | package.name = accelerometer | ||
+ | package.domain = org.kivy | ||
+ | source.dir = . | ||
+ | source.include_exts = py, | ||
+ | requirements = python3, | ||
+ | android.permissions = INTERNET, WRITE_EXTERNAL_STORAGE, | ||
+ | services = Pong: | ||
+ | </ | ||
+ | |||
+ | ===main.py=== | ||
+ | [[https:// | ||
+ | |||
+ | <code python> | ||
+ | from jnius import autoclass | ||
+ | """ | ||
+ | Dans buildozer.spec | ||
+ | package.name = accelerometer | ||
+ | package.domain = org.kivy | ||
+ | services = Pong: | ||
+ | |||
+ | SERVICE_NAME = u' | ||
+ | packagename=u' | ||
+ | servicename=u' | ||
+ | |||
+ | Structure = package.domain.package.name.ServiceToto | ||
+ | package.domain = org.kivy | ||
+ | package.name = accelerometer | ||
+ | soit | ||
+ | org.kivy.accelerometer.ServicePong | ||
+ | """ | ||
+ | |||
+ | SERVICE_NAME = ' | ||
+ | print(" | ||
+ | |||
+ | ..... | ||
+ | |||
+ | class AccelerometerApp(App): | ||
+ | |||
+ | ... | ||
+ | |||
+ | def start_service(self): | ||
+ | if ANDROID: | ||
+ | self.service = autoclass(SERVICE_NAME) | ||
+ | self.m_activity = autoclass(u' | ||
+ | argument = '' | ||
+ | self.service.start(self.m_activity, | ||
+ | |||
+ | ... | ||
+ | |||
+ | def do_quit(self): | ||
+ | if ANDROID: | ||
+ | self.service.stop(self.m_activity) | ||
+ | self.service = None | ||
+ | else: | ||
+ | self.client.send_message(b'/ | ||
+ | sleep(1) | ||
+ | |||
+ | AccelerometerApp.get_running_app().stop() | ||
+ | </ | ||
+ | |||
+ | ===service.py=== | ||
+ | [[https:// | ||
+ | |||
+ | <code python> | ||
+ | class AccelerometerService: | ||
+ | |||
+ | def __init__(self): | ||
+ | |||
+ | ... | ||
+ | self.init_osc() | ||
+ | self.sensor_init() | ||
+ | |||
+ | ... | ||
+ | |||
+ | def init_osc(self): | ||
+ | """ | ||
+ | de la même machine, donc il faut un client. | ||
+ | """ | ||
+ | self.server = OSCThreadServer() | ||
+ | self.server.listen(' | ||
+ | # Les callbacks du serveur | ||
+ | self.server.bind(b'/ | ||
+ | self.server.bind(b'/ | ||
+ | self.server.bind(b'/ | ||
+ | # Un simple client | ||
+ | self.client = OSCClient(b' | ||
+ | |||
+ | ... | ||
+ | |||
+ | def run(self): | ||
+ | while self.loop: | ||
+ | self.get_acceleration() | ||
+ | sleep(0.02) | ||
+ | |||
+ | |||
+ | if __name__ == ' | ||
+ | |||
+ | ACCELEROMETER = AccelerometerService() | ||
+ | ACCELEROMETER.run() | ||
+ | </ | ||
+ | |||
+ | |||
+ | =====Un projet plus complet===== | ||
+ | |||
+ | * **[[https:// | ||
{{tag> kivy python sb }} | {{tag> kivy python sb }} |
kivy_android_service.1604054117.txt.gz · Dernière modification : 2020/10/30 10:35 de serge