Using Python for Android and QPython
Going Further with Geofix
Another example of QPython's possibilities is the Geofix project [9] also cobbled together by yours truly. The core of Geofix is a QPython script (there is also a version for use with Python for Android) that receives the geographical coordinates of the current location and saves the obtained data in a tab-separated text file and an SQLite database. The script itself is too long to list in this article, but you can easily clone the project's GitHub repository and study it to your heart's content.
The Geofix script makes use of several facades. In the code snippet below, the startLocating
and stopLocating
facades are used to start and stop geolocation:
droid.startLocating() droid.eventWaitFor('location', int(wait)) location = droid.readLocation().result droid.stopLocating()
The eventWaitFor
facade is used to pause the script until it receives geographical data, then the readLocation
facade reads the obtained data. The Geofix script also uses the dialogCreateAlert
and dialogGetResponse
facades to create a dialog and read user response. The cameraInteractiveCapturePicture
facade is used to take a snapshot with the default camera app.
In addition to the .tsv
text file, the script saves the obtained geographical data in an SQLite database, and the accompanying simple web app makes it possible to view data in a browser. You can deploy the web app on a remote server and modify the upload script in Listing 2 to transfer the geofix.sqlite
database to the appropriate folder on the server via FTP.
Put on a Web Facade
SL4A provides several facades for creating graphical elements like dialogs, input boxes, alerts, and so on, but, that's not all: The scripting layer also features the webViewShow
facade, which makes it possible to create advanced interfaces using a mix of HTML, CSS, and JavaScript. Although this topic deserves a separate article, the code in Listing 3 gives a general idea of how the webViewShow
facade works.
Listing 3
Where Am I Script
The first part of the script obtains the geographical coordinates of the current location and performs reverse geocoding (i.e., determines the city and country name using the supplied latitude and longitude). The script then writes a simple HTML template containing the place
variable to the whereami.html
file. Then, the webViewShow
facade opens the generated HTML page for viewing.
Final Word
Because they are lightweight and easy to master, SL4A/Python for Android and QPython provide a relatively easy way to automate tasks and build Android apps. Even if your Python skills are rather modest, you should be able to learn the Android-specific scripting basics in no time.
This article barely scratched the surface of the Android scripting layer, but I hope it gives you an idea of what you can build using either the Python for Android or QPython framework.
Infos
- Scripting Layer for Android: http://code.google.com/p/android-scripting/
- Android 5.x-compatible SL4A: http://github.com/kuri65536/sl4a
- Android 5.x-compatible Python for Android: http://github.com/kuri65536/python-for-android
- QPython project: http://qpython.org
- QPython on Google Play Store: http://play.google.com/store/apps/details?id=com.hipipal.qpyplus
- QPython3 on Google Play Store: http://play.google.com/store/apps/details?id=com.hipipal.qpy3
- SL4A API reference: http://code.google.com/p/android-scripting/wiki/ApiReference
- Mejiro: http://dmpop.github.io/mejiro
- Geofix: http://github.com/dmpop/geofix
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.