1
1
2
+ Working on Android
3
+ ==================
4
+
5
+ This page gives details on accessing Android APIs and managing other
6
+ interactions on Android.
7
+
8
+
2
9
Accessing Android APIs
3
- ======================
10
+ ----------------------
4
11
5
12
When writing an Android application you may want to access the normal
6
13
Android Java APIs, in order to control your application's appearance
@@ -27,7 +34,7 @@ recommended for use in new applications.
27
34
28
35
29
36
Using Pyjnius
30
- -------------
37
+ ~~~~~~~~~~~~~
31
38
32
39
Pyjnius lets you call the Android API directly from Python Pyjnius is
33
40
works by dynamically wrapping Java classes, so you don't have to wait
@@ -83,7 +90,7 @@ You can check the `Pyjnius documentation <Pyjnius_>`_ for further details.
83
90
84
91
85
92
Using Plyer
86
- -----------
93
+ ~~~~~~~~~~~
87
94
88
95
Plyer provides a much less verbose, Pythonic wrapper to
89
96
platform-specific APIs. It supports Android as well as iOS and desktop
@@ -107,7 +114,7 @@ This is obviously *much* less verbose than with Pyjnius!
107
114
108
115
109
116
Using ``android ``
110
- -----------------
117
+ ~~~~~~~~~~~~~~~~~
111
118
112
119
This Cython module was used for Android API interaction with Kivy's old
113
120
interface, but is now mostly replaced by Pyjnius.
@@ -147,3 +154,51 @@ code::
147
154
148
155
import webbrowser
149
156
webbrowser.register('android', AndroidBrowser, None, -1)
157
+
158
+
159
+ Working with the App lifecycle
160
+ ------------------------------
161
+
162
+ Handling the back button
163
+ ~~~~~~~~~~~~~~~~~~~~~~~~
164
+
165
+ Android phones always have a back button, which users expect to
166
+ perform an appropriate in-app function. If you do not handle it, Kivy
167
+ apps will actually shut down and appear to have crashed.
168
+
169
+ In SDL2 bootstraps, the back button appears as the escape key (keycode
170
+ 27, codepoint 270). You can handle this key to perform actions when it
171
+ is pressed.
172
+
173
+ For instance, in your App class in Kivy::
174
+
175
+ from kivy.core.window import Window
176
+
177
+ class YourApp(App):
178
+
179
+ def build(self):
180
+ Window.bind(on_keyboard=self.key_input)
181
+ return Widget() # your root widget here as normal
182
+
183
+ def key_input(self, window, key, scancode, codepoint, modifier):
184
+ if key == 27:
185
+ return True # override the default behaviour
186
+ # the key now does nothing
187
+ return False
188
+
189
+
190
+ Pausing the App
191
+ ~~~~~~~~~~~~~~~
192
+
193
+ When the user leaves an App, it is automatically paused by Android,
194
+ although it gets a few seconds to store data etc. if necessary. Once
195
+ paused, there is no guarantee that your app will run again.
196
+
197
+ With Kivy, add an ``on_pause `` method to your App class, which returns True::
198
+
199
+ def on_pause(self):
200
+ return True
201
+
202
+ With the webview bootstrap, pausing should work automatically.
203
+
204
+ Under SDL2, you can handle the `appropriate events <https://wiki.libsdl.org/SDL_EventType >`__ (see SDL_APP_WILLENTERBACKGROUND etc.).
0 commit comments