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
-
elementary OS 7.1 Now Available for Download
The team behind elementary OS has released the latest version of its operating system with a focus on personalization, inclusivity, accessibility, and privacy.
-
The GNU Project Celebrates Its 40th Birthday
September 27 marks the 40th anniversary of the GNU Project, and it was celebrated with a hacker meeting in Biel/Bienne, Switzerland.
-
Linux Kernel Reducing Long-Term Support
LTS support for the Linux kernel is about to undergo some serious changes that will have a considerable impact on the future.
-
Fedora 39 Beta Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.