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.
Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.
- 1Linux Top 3: GNOME 3.12 and New Betas for Ubuntu 14.04 and OpenMandriva Lx 2014.0
- 2Linux Top 3: Linus Lashes out, Linux 3.14 Gets PIE and Ubuntu One is Done.
- 3Linux Top 3: Ubuntu 14.04, Debian Gives Squeeze More Life and Red Hat Goes Atomic
- 4Linux Top 3: CoreOS, Oracle Enterprise Linux 7 and Ubuntu 14.10
- 5Linux Top 3: Debian Gives Up on Upstart, Ubuntu and Linux Kernel Updates