April 22, 2019

Automating your Android Phone with SL4A - page 2

First Example

  • September 2, 2010
  • By Paul Ferrill
Twitter seems to be a reasonable starting place for building a little script to broadcast your current location for all your followers to see. While this might seem odd or even harmful to some, it could be quite useful for tracking your arrival times and location at specific work sites or even a way to let loved ones know you arrived safely with a single touch on the name of the script.

<em>figure 2</em>
figure 2

If you plan on coding on your desktop and using the adb tool to connect to your phone, you'll need to do a few things first. The Python interpreter you get with SL4A has a number of packages loaded that you'll need to install on your host machine to test code remotely. For this example you'll need to download twitter.py from the Google code site. Once you have the file downloaded, you'll need to type in a few lines in a terminal window as follows:

$ tar -xvf python-twitter-0.6.tar.gz

$ cd python-twiter-0.6

$ sudo python setup.py install

This will install the code you'll need to use twitter.py in your local python directory. Now we're ready to write some code. There are two methods of determining your current location on the Android platform--namely GPS and from the network. You have to start the phone's locating function and read your position with the following code:



loc = droid.readLocation()


This will first turn on the locating routines, wait a while (sleep(15) waits 15 seconds), read the location and then turn the locating routines back off. You could also use the getLastKnownLocation routine, but you run the risk of having old information. The basics of a twitter app are pretty straightforward as shown below:

import android, twitter, datetime, time

droid = android.Android()

api = twitter.Api("username", "password")



loc = droid.readLocation()


now = str(datetime.datetime.now())

lat = str(loc[1]['gps']['latitude'])

lon = str(loc[1]['gps']['longitude'])

outString = now + ' ' + lat + ' ' + lon


That's pretty much all there is to it. You'll need to enter your twitter username and password in the appropriate place. All that's left is getting the script on your device. You can either use the adb utility from a terminal window to accomplish this last task or the Dalvik Debug Monitor (DDMS). DDMS is definitely an easier route and gives you a graphical window similar to Nautilus to display the files on your device (or emulator) and pull or push files from the device. It's also handy for snapping screen shots from the device.

Once you have the script copied to the device, you'll be able to tweet your location as often as you'd like by touching the tweetmyloc.py in your scripts folder. The resulting twitter entry won't look very pretty, but it has the information you want.

Wrapping Up

SL4A is a slick application for building useful scripts to automate things on your Android device. While it's probably not something your average user would want to try, it should really appeal to Python programmers with an itch to write code for their shiny new phone.

Most Popular LinuxPlanet Stories