Python For Android
Python For Android
Release 1.2
Mathieu Virbel
Contents
Toolchain
1.1 Introduction . . . . . . . .
1.2 How does it work ? . . . . .
1.3 Prerequisites . . . . . . . .
1.4 Usage . . . . . . . . . . . .
1.5 Meta-data . . . . . . . . . .
1.6 Customize your distribution
.
.
.
.
.
.
3
3
3
3
5
7
7
Examples
2.1 Prebuilt VirtualBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Hello world . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3 Compass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
13
13
14
Python API
3.1 Android (android) . . . . . . . . .
3.2 Activity (android.activity) . .
3.3 Billing (android.billing) . . .
3.4 Broadcast (android.broadcast)
3.5 Mixer (android.mixer) . . . . .
3.6 Runnable (android.runnable) .
3.7 Service (android.service) . . .
.
.
.
.
.
.
.
19
19
19
20
21
22
23
23
25
25
26
27
Contribute
5.1 Extending Python for android native support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2 Create your own recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
29
30
Related projects
31
FAQ
7.1 arm-linux-androideabi-gcc: Internal error: Killed (program cc1) . . . . . . . . . . . . . . . . . . . .
7.2 _sqlite3.so not found . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33
33
33
35
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
ii
37
Python for android is a project to create your own Python distribution including the modules you want, and create an
apk including python, libs, and your application.
Forum: https://groups.google.com/forum/#!forum/python-android
Mailing list: python-android@googlegroups.com
Contents
Contents
CHAPTER 1
Toolchain
1.1 Introduction
In terms of comparaison, you can check how Python for android can be useful compared to other projects.
Project
Python for android
PGS4A
Android scripting
Python on a chip
Native Python
Yes
Yes
No
No
GUI libraries
Yes
Yes
No
No
APK generation
Yes
Yes
No
No
Custom build
Yes
No
No
No
Note: For the moment, we are shipping only one java bootstrap needed for decompressing all the files of your
project, create an OpenGL ES 2.0 surface, handle touch input and manage an audio thread.
If you want to use it without kivy module (an opengl es 2.0 ui toolkit), then you might want a lighter java bootstrap,
that we dont have right now. Help is welcome :)
1.3 Prerequisites
Note: There is a VirtualBox Image we provide with the prerequisites along with Android SDK and NDK preinstalled
to ease your installation woes. You can download it from here.
3
Warning: The current version is tested only on Ubuntu oneiric (11.10) and precise (12.04). If it doesnt work on
other platforms, send us patch, not bug report.
You need the minimal environment for building python. Note that other libraries might need other tools (cython is
used by some recipes, and ccache to speedup the build):
sudo apt-get install build-essential patch git-core ccache ant python-pip python-dev
If you are on a 64 bit distro, you should install these packages too :
sudo apt-get install ia32-libs
libc6-dev-i386
You must have android SDK and NDK. Right now, its prefered to use:
SDK API 8 or 14 (15 will not work until a new NDK is released)
NDK r5b or r7
You can download them at:
http://developer.android.com/sdk/index.html
http://developer.android.com/sdk/ndk/index.html
If its your very first time into android SDK, dont forget to follow documentation for recommended components at:
http://developer.android.com/sdk/installing/adding-packages.html
You need to download at least one platform into your environment, so
that you will be able to compile your application and set up an Android
Virtual Device (AVD) to run it on (in the emulator). To start with,
just download the latest version of the platform. Later, if you plan to
publish your application, you will want to download other platforms as
well, so that you can test your application on the full range of
Android platform versions that your application supports.
After installing them, export both installation path, NDK version and API to use:
export
export
export
export
ANDROIDSDK=/path/to/android-sdk
ANDROIDNDK=/path/to/android-ndk
ANDROIDNDKVER=rX
ANDROIDAPI=X
# example
export ANDROIDSDK="/home/tito/code/android/android-sdk-linux_86"
export ANDROIDNDK="/home/tito/code/android/android-ndk-r7"
export ANDROIDNDKVER=r7
export ANDROIDAPI=14
Also, you must configure youre PATH to add the android binary:
Chapter 1. Toolchain
export PATH=$ANDROIDNDK:$ANDROIDSDK/platform-tools:$ANDROIDSDK/tools:$PATH
1.4 Usage
1.4.1 Step 1: compile the toolchain
If you want to compile the toolchain with only kivy module:
./distribute.sh -m "kivy"
After a long time, youll get a dist/default directory containing all the compiled libraries and build.py script to
package your application using thoses libraries.
You can include other modules (or recipes) to compile using -m:
./distribute.sh -m "openssl kivy"
./distribute.sh -m "pil ffmpeg kivy"
You can also specify a specific version for each package. Please note that the compilation might break if you dont
use the default version. Most recipes have patch to fixes android issues, and might not apply if you specify a version.
We also recommend to clean build/ before changing version.:
./distribute.sh -m "openssl kivy==master"
Note: Recipes download a defined version of their needed package from the internet, and build from it, if you
know what you are doing, and want to override that, you can export the env variable P4A_recipe_name_DIR and this
directory will be copied and used instead.
Available options to distribute.sh:
-d directory
-h
-l
-m mod1 mod2
-f
-u mod1 mod2
1.4. Usage
Chapter 1. Toolchain
1.5 Meta-data
New in version 1.3.
You can extend the AndroidManifest.xml with application meta-data. If you are using external toolkits like Google
Maps, you might want to set your API key in the meta-data. You could do it like this:
./build.py ... --meta-data com.google.android.maps.v2.API_KEY=YOURAPIKEY
Some meta-data can be used to interact with the behavior of our internal component.
Token
surface.transparent
surface.depth
Description
If set to 1, the created surface will be transparent (can be used to add background Android
widget in the background, or use accelerated widgets)
Size of the depth component, default to 0. 0 means automatic, but you can force it to a specific
value. Be warned, some old phone might not support the depth you want.
surface.stencil
Size of the stencil component, default to 8.
anColor (32bits RGBA color), used for the background window. Usually, the background is
droid.background_color
covered by the OpenGL Background, unless surface.transparent is set.
(*) fake package that will contain all the python installation
(*) fake package that will contain your application
contain all the apk generated from build.py
list of file patterns to not include in the APK
internals libraries for build.py
build script to use for packaging your application
(*) build settings (generated from templates)
settings generated from your distribute.sh
contain all the compiled libraries
settings generated from your distribute.sh
private directory containing all the python files
this is where you can remove or add python libs.
by default, some modules are already removed (tests, idlelib, ...)
settings generated from your distribute.sh
the whole python installation, generated from distribute.sh
not included in the final package.
(*) android resource (generated from build.py)
Java bootstrap
Templates used by build.py
(*): Theses files are automatically generated from build.py, dont change them directly !
1.6.1 Prerequisites
Note: There is a VirtualBox Image we provide with the prerequisites along with Android SDK and NDK preinstalled
to ease your installation woes. You can download it from here.
1.5. Meta-data
Warning: The current version is tested only on Ubuntu oneiric (11.10) and precise (12.04). If it doesnt work on
other platforms, send us patch, not bug report.
You need the minimal environment for building python. Note that other libraries might need other tools (cython is
used by some recipes, and ccache to speedup the build):
sudo apt-get install build-essential patch git-core ccache ant python-pip python-dev
If you are on a 64 bit distro, you should install these packages too :
sudo apt-get install ia32-libs
libc6-dev-i386
You must have android SDK and NDK. Right now, its prefered to use:
SDK API 8 or 14 (15 will not work until a new NDK is released)
NDK r5b or r7
You can download them at:
http://developer.android.com/sdk/index.html
http://developer.android.com/sdk/ndk/index.html
If its your very first time into android SDK, dont forget to follow documentation for recommended components at:
http://developer.android.com/sdk/installing/adding-packages.html
You need to download at least one platform into your environment, so
that you will be able to compile your application and set up an Android
Virtual Device (AVD) to run it on (in the emulator). To start with,
just download the latest version of the platform. Later, if you plan to
publish your application, you will want to download other platforms as
well, so that you can test your application on the full range of
Android platform versions that your application supports.
After installing them, export both installation path, NDK version and API to use:
export
export
export
export
ANDROIDSDK=/path/to/android-sdk
ANDROIDNDK=/path/to/android-ndk
ANDROIDNDKVER=rX
ANDROIDAPI=X
# example
export ANDROIDSDK="/home/tito/code/android/android-sdk-linux_86"
export ANDROIDNDK="/home/tito/code/android/android-ndk-r7"
export ANDROIDNDKVER=r7
export ANDROIDAPI=14
Also, you must configure youre PATH to add the android binary:
export PATH=$ANDROIDNDK:$ANDROIDSDK/platform-tools:$ANDROIDSDK/tools:$PATH
Chapter 1. Toolchain
1.6.2 Usage
Step 1: compile the toolchain
If you want to compile the toolchain with only kivy module:
./distribute.sh -m "kivy"
After a long time, youll get a dist/default directory containing all the compiled libraries and build.py script to
package your application using thoses libraries.
You can include other modules (or recipes) to compile using -m:
./distribute.sh -m "openssl kivy"
./distribute.sh -m "pil ffmpeg kivy"
You can also specify a specific version for each package. Please note that the compilation might break if you dont
use the default version. Most recipes have patch to fixes android issues, and might not apply if you specify a version.
We also recommend to clean build/ before changing version.:
./distribute.sh -m "openssl kivy==master"
Note: Recipes download a defined version of their needed package from the internet, and build from it, if you
know what you are doing, and want to override that, you can export the env variable P4A_recipe_name_DIR and this
directory will be copied and used instead.
Available options to distribute.sh:
-d directory
-h
-l
-m mod1 mod2
-f
-u mod1 mod2
Warning: Some files and modules for python are blacklisted by default to save a few megabytes on the final apk
file, in case your applications doesnt find a standard python module, check the src/blacklist.txt file remove the
module you need from the list, and try again.
Available options to build.py:
-h, --help
--package PACKAGE
1.6.3 Meta-data
New in version 1.3.
You can extend the AndroidManifest.xml with application meta-data. If you are using external toolkits like Google
Maps, you might want to set your API key in the meta-data. You could do it like this:
10
Chapter 1. Toolchain
Some meta-data can be used to interact with the behavior of our internal component.
Token
surface.transparent
surface.depth
Description
If set to 1, the created surface will be transparent (can be used to add background Android
widget in the background, or use accelerated widgets)
Size of the depth component, default to 0. 0 means automatic, but you can force it to a specific
value. Be warned, some old phone might not support the depth you want.
surface.stencil
Size of the stencil component, default to 8.
anColor (32bits RGBA color), used for the background window. Usually, the background is
droid.background_color
covered by the OpenGL Background, unless surface.transparent is set.
(*) fake package that will contain all the python installation
(*) fake package that will contain your application
contain all the apk generated from build.py
list of file patterns to not include in the APK
internals libraries for build.py
build script to use for packaging your application
(*) build settings (generated from templates)
settings generated from your distribute.sh
contain all the compiled libraries
settings generated from your distribute.sh
private directory containing all the python files
this is where you can remove or add python libs.
by default, some modules are already removed (tests, idlelib, ...)
settings generated from your distribute.sh
the whole python installation, generated from distribute.sh
not included in the final package.
(*) android resource (generated from build.py)
Java bootstrap
Templates used by build.py
(*): Theses files are automatically generated from build.py, dont change them directly !
11
12
Chapter 1. Toolchain
CHAPTER 2
Examples
13
on_release: root.my_callback()
)
class HelloWorldScreen(GridLayout):
counter = NumericProperty(0)
def my_callback(self):
print The button has been pushed
self.counter += 1
class HelloWorldApp(App):
def build(self):
return HelloWorldScreen()
if __name__ == __main__:
HelloWorldApp().run()
7. Plug your android device, and ensure you can install development application
8. Build your hello world application in debug mode:
./build.py --package org.hello.world --name "Hello world" \
--version 1.0 --dir /PATH/TO/helloworld debug installd
2.3 Compass
The following example is an extract from the Compass app as provided in the Kivy examples/android/compass folder:
14
Chapter 2. Examples
# ... imports
Hardware = autoclass(org.renpy.android.Hardware)
class CompassApp(App):
needle_angle = NumericProperty(0)
def build(self):
self._anim = None
Hardware.magneticFieldSensorEnable(True)
Clock.schedule_interval(self.update_compass, 1 / 10.)
def update_compass(self, *args):
# read the magnetic sensor from the Hardware class
(x, y, z) = Hardware.magneticFieldSensorReading()
# calculate the angle
needle_angle = Vector(x , y).angle((0, 1)) + 90.
# animate the needle
if self._anim:
self._anim.stop(self)
self._anim = Animation(needle_angle=needle_angle, d=.2, t=out_quad)
self._anim.start(self)
def on_pause(self):
# when you are going on pause, dont forget to stop the sensor
Hardware.magneticFieldSensorEnable(False)
return True
def on_resume(self):
# reactivate the sensor when you are back to the app
Hardware.magneticFieldSensorEnable(True)
if __name__ == __main__:
CompassApp().run()
If you compile this app, you will get an APK which outputs the following screen:
Figure 2.1: Screenshot of the Kivy Compass App (Source of the Compass Windrose: Wikipedia)
2.3. Compass
15
7. Plug your android device, and ensure you can install development application
16
Chapter 2. Examples
2.3.2 Compass
The following example is an extract from the Compass app as provided in the Kivy examples/android/compass folder:
# ... imports
Hardware = autoclass(org.renpy.android.Hardware)
class CompassApp(App):
needle_angle = NumericProperty(0)
def build(self):
self._anim = None
Hardware.magneticFieldSensorEnable(True)
Clock.schedule_interval(self.update_compass, 1 / 10.)
def update_compass(self, *args):
# read the magnetic sensor from the Hardware class
(x, y, z) = Hardware.magneticFieldSensorReading()
# calculate the angle
needle_angle = Vector(x , y).angle((0, 1)) + 90.
# animate the needle
if self._anim:
self._anim.stop(self)
self._anim = Animation(needle_angle=needle_angle, d=.2, t=out_quad)
self._anim.start(self)
def on_pause(self):
# when you are going on pause, dont forget to stop the sensor
Hardware.magneticFieldSensorEnable(False)
return True
2.3. Compass
17
def on_resume(self):
# reactivate the sensor when you are back to the app
Hardware.magneticFieldSensorEnable(True)
if __name__ == __main__:
CompassApp().run()
If you compile this app, you will get an APK which outputs the following screen:
Figure 2.2: Screenshot of the Kivy Compass App (Source of the Compass Windrose: Wikipedia)
18
Chapter 2. Examples
CHAPTER 3
Python API
The Python for android project includes a Python module called android. It consists of multiple parts which are
mostly there to facilitate the use of the Java API.
This module is not designed to be comprehensive. Most of the Java API is also accessible with PyJNIus, so if you
cant find what you need here you can try using the Java API directly instead.
19
rawmsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
if not rawmsgs:
return
for message in rawmsgs:
message = cast(NdefMessage, message)
payload = message.getRecords()[0].getPayload()
print payload: {}.format(.join(map(chr, payload)))
def nfc_enable(self):
activity.bind(on_new_intent=self.on_new_intent)
# ...
def nfc_disable(self):
activity.unbind(on_new_intent=self.on_new_intent)
# ...
3. Setup some In-App product to buy. Lets say youve created a product with the id org.kivy.gopremium
4. In your application, you can use the billing module like this:
from android.billing import BillingService
from kivy.clock import Clock
class MyBillingService(object):
def __init__(self):
super(MyBillingService, self).__init__()
# Start the billing service, and attach our callback
self.service = BillingService(billing_callback)
# Start a clock to check billing service message every second
Clock.schedule_interval(self.service.check, 1)
def billing_callback(self, action, *largs):
Callback that will receive all the events from the Billing service
if action == BillingService.BILLING_ACTION_ITEMSCHANGED:
items = largs[0]
if org.kivy.gopremium in items:
print "Congratulations, you have a premium acess"
else:
print "Unfortunately, you dont have premium access"
def buy(self, sku):
# Method to buy something.
self.service.buy(sku)
20
def get_purchased_items(self):
# Return all the items purchased
return self.service.get_purchased_items()
6. Youll receive all the notifications about the billing process in the callback.
7. Last step, create your application with with-billing $BILLING_PUBKEY:
./build.py ... --with-billing $BILLING_PUBKEY
start()
Register the receiver with all the actions and categories, and start handling events.
stop()
Unregister the receiver with all the actions and categories, and stop handling events.
Example:
class TestApp(App):
def build(self):
self.br = BroadcastReceiver(
21
self.on_broadcast, actions=[headset_plug])
self.br.start()
# ...
def on_broadcast(self, context, intent):
extras = intent.getExtras()
headset_state = bool(extras.get(state))
if headset_state:
print The headset is plugged
else:
print The headset is unplugged
# Dont forget to stop and restart the receiver when the app is going
# to pause / resume mode
def on_pause(self):
self.br.stop()
return True
def on_resume(self):
self.br.start()
Note that if youre using the kivy.core.audio module, you dont have to do anything, it is all automatic.
The android.mixer module is a wrapper around the Android MediaPlayer class. This allows it to take advantage of
any hardware acceleration present, and also eliminates the need to ship codecs as part of an application.
It has several differences with the pygame mixer:
The init() and pre_init() methods work, but are ignored - Android chooses appropriate settings automatically.
Only filenames and true file objects can be used - file-like objects will probably not work.
Fadeout does not work - it causes a stop to occur.
Looping is all or nothing, there is no way to choose the number of loops that occur. For looping to work, the
android.mixer.periodic() function should be called on a regular basis.
Volume control is ignored.
End events are not implemented.
The mixer.music object is a class (with static methods on it), rather than a module. Calling methods like
mixer.music.play() should work.
22
23
24
CHAPTER 4
Using PyJNIus to access the Android API restricts the usage to a simple call of the autoclass constructor function and
a second call to instantiate this class.
You can access through this method all Java Android API, e.g. to get the DisplayMetrics of an Android device could
fetched using the following piece of code:
DisplayMetrics = autoclass(android.util.DisplayMetrics)
metrics = DisplayMetrics()
metrics.setToDefaults()
self.densityDpi = metrics.densityDpi
You can access all fields and methods as described in the Java Android DisplayMetrics API as shown here with the
method setToDefaults() and the field densityDpi. Before you use o view a field, you should always call setToDefaults
to initiate to the default values of the device.
Currently only JavaMethod, JavaStaticMethod, JavaField, JavaStaticField and JavaMultipleMethod are built into
PyJNIus, therefore such constructs like registerListener or something like this have to be coded still in Java. For
this the Android module described below is available to access some of the hardware in Android devices.
4.1 Activity
If you want the instance of the current Activity, use:
PythonActivity.mActivity if you are running an application
PythonService.mService if you are running a service
class org.renpy.android.PythonActivity
mInfo
Instance of an ApplicationInfo
mActivity
Instance of PythonActivity.
registerNewIntentListener(NewIntentListener listener)
Register a new instance of NewIntentListener to be called when onNewIntent is called.
unregisterNewIntentListener(NewIntentListener listener)
Unregister a previously registered listener from registerNewIntentListener()
registerActivityResultListener(ActivityResultListener listener)
Register a new instance of ActivityResultListener to be called when onActivityResult is called.
25
unregisterActivityResultListener(ActivityResultListener listener)
Unregister a previously registered listener from PythonActivity.registerActivityResultListener()
class org.renpy.android.PythonActivity_ActivityResultListener
Note:
This class is a subclass of
PythonActivity$ActivityResultListener
PythonActivity,
so
the
notation
will
be
Listener interface for onActivityResult. You need to implementing it, create an instance and use it with
PythonActivity.registerActivityResultListener().
onActivityResult(int requestCode, int resultCode, Intent data)
Method to implement
class org.renpy.android.PythonActivity_NewIntentListener
Note:
This class is a subclass
PythonActivity$NewIntentListener
of
PythonActivity,
so
the
notation
will
be
Listener interface for onNewIntent. You need to implementing it, create an instance and use it with
registerNewIntentListener().
onNewIntent(Intent intent)
Method to implement
4.2 Action
class org.renpy.android.Action
This module is built to deliver data to someone else.
send(mimetype, filename, subject, text, chooser_title)
Deliver data to someone else. This method is a wrapper around ACTION_SEND
Parameters
mimetype: str Must be a valid mimetype, that represent the content to sent.
filename: str, default to None (optional) Name of the file to attach. Must be a absolute
path.
subject: str, default to None (optional) Default subject
text: str, default to None (optional) Content to send.
chooser_title: str, default to None (optional) Title of the android chooser window, default
to Send email...
Sending a simple hello world text:
android.action_send(text/plain, text=Hello world,
subject=Test from python)
26
4.3 Hardware
class org.renpy.android.Hardware
This module is built for accessing hardware devices of an Android device. All the methods are static and public,
you dont need an instance.
vibrate(s)
Causes the phone to vibrate for s seconds. This requires that your application have the VIBRATE permission.
getHardwareSensors()
Returns a string of all hardware sensors of an Android device where each line lists the informations about
one sensor in the following format:
Name=name,Vendor=vendor,Version=version,MaximumRange=maximumRange,MinDelay=minDelay,Power=power,Type=
For more information about this informations look into the original Java API for the Sensors Class
accelerometerSensor
This variable links to a generic3AxisSensor instance and their functions to access the accelerometer sensor
orientationSensor
This variable links to a generic3AxisSensor instance and their functions to access the orientation sensor
magenticFieldSensor
The following two instance methods of the generic3AxisSensor class should be used to enable/disable the sensor
and to read the sensor
changeStatus(boolean enable)
Changes the status of the sensor, the status of the sensor is enabled, if enable is true or disabled, if enable
is false.
readSensor()
Returns an (x, y, z) tuple of floats that gives the sensor reading, the units depend on the sensor as shown
on the Java API page for SensorEvent. The sesnor must be enabled before this function is called. If the
tuple contains three zero values, the accelerometer is not enabled, not available, defective, has not returned
a reading, or the device is in free-fall.
get_dpi()
Returns the screen density in dots per inch.
show_keyboard()
Shows the soft keyboard.
hide_keyboard()
Hides the soft keyboard.
wifi_scanner_enable()
Enables wifi scanning.
Note: ACCESS_WIFI_STATE and CHANGE_WIFI_STATE permissions are required.
4.3. Hardware
27
wifi_scan()
Returns a String for each visible WiFi access point
(SSID, BSSID, SignalLevel)
28
CHAPTER 5
Contribute
5.1.1 Step 1: write the java code to create very simple functions to use
like : accelerometer Enable/Reading In our project, this is done in the Hardware.java: https://github.com/kivy/pythonfor-android/blob/master/src/src/org/renpy/android/Hardware.java you can see how its implemented
5.1.3 Step 3 : you have the java part, that you can call from the C
You can now do the Python extension around it, all the android python part is done in https://github.com/kivy/pythonfor-android/blob/master/recipes/android/src/android.pyx
[python] android.accelerometer_reading
ware.accelerometer_reading()
[C]
android_accelerometer_reading
[Java]
Hard-
The jni part is really a C api to call java methods. a little bit hard to get it with the syntax, but working with current
example should be ok
29
http://code.google.com/p/android-scripting/source/browse/android/BluetoothFacade/src/com/googlecode/android_scripting/facade/Blue
You can learn from it, and see how is its can be used as is, or if you can simplify / remove stuff you dont want.
From this point, create a bluetooth file in python-for-android/tree/master/src/src/org/renpy/android in Java.
Do a good API (enough simple to be able to write the jni in a very easy manner, like, dont pass any custom java object
in argument).
Then write the JNI, and then the python part.
3 steps, once you get it, the real difficult part is to write the java part :)
Then, edit the sdl/recipe.sh to adjust other information (version, url) and complete build function.
30
Chapter 5. Contribute
CHAPTER 6
Related projects
31
32
CHAPTER 7
FAQ
Go to
33
34
Chapter 7. FAQ
CHAPTER 8
genindex
modindex
search
35
36
a
android, 19
android.activity, 19
android.billing, 20
android.broadcast, 21
android.mixer, 22
android.runnable, 23
android.service, 23
o
org.renpy.android, 25
37
38
Index
Symbols
magenticFieldSensor
(org.renpy.android.Hardware unbind() (in module android.activity), 19
unregisterActivityResultListener()
attribute), 27
(org.renpy.android.PythonActivity method), 26
map_key() (in module android), 19
39
unregisterNewIntentListener()
(org.renpy.android.PythonActivity method), 25
V
vibrate() (org.renpy.android.Hardware method), 27
W
wait_for_resume() (in module android), 19
wifi_scan() (org.renpy.android.Hardware method), 27
wifi_scanner_enable()
(org.renpy.android.Hardware
method), 27
40
Index