Automating your Android Phone with SL4A - page 2
First ExampleTwitter 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.
$ 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['gps']['latitude'])
lon = str(loc['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.
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.
Sponsored by BlackBerry
BlackBerry® Enterprise Server Express enables businesses of any size to quickly and easily get started with the BlackBerry solution. It provides advanced BlackBerry smartphone features with no additional software or user license fees, and works with any Internet-enabled BlackBerry data plan or a BlackBerry enterprise data plan. Download now!