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.
News
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.