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.eventWaitFor('location', int(wait))
location = droid.readLocation().result

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.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
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