March 24, 2019

Automating your Android Phone with SL4A

First Example

  • September 2, 2010
  • By Paul Ferrill

In the first installment of this article we looked at getting Google's Scripting Layer for Android (SL4A) downloaded and installed on your Android phone. We examined the basics of writing scripts using Python and even included a short script to set a few of the profile settings. This time we'll take a look at some of the sample scripts found on the SL4A website and talk about how you might write a script of your own.

<em>figure 1</em>
figure 1

The first thing you should do after installing the SL4A application on your phone is to add a shortcut to the scripts folder on one of the home screens. On the HTC EVO you can do this by pressing on an empty space and hold until the Add to Home dialog appears. From here you can add a Widget, App, Shortcut or Folder. Selecting Folder presents a list of available folders and an option to create a New Folder. Selecting the Scripts folder will add a folder icon to the current home screen. Once that's done, you'll have single-click access to any of the scripts stored on your SD card in the sl4a/scripts directory.

Useful Samples

It seems as though there is a universal rule when teaching a new programming language that you must create a program to output the phrase "Hello World". SL4A holds to this premise and includes a link to a version of this program on the Wiki Tutorials page. If you take a look at some of the examples on that page, you'll see a number of items with potential uses like Twitter clients, talking weather forecast, a silent mode trigger to set the phone to vibrate during sleeping hours and more. We'll take a look at a few of those samples to help explore the possibilities.

One of the really useful examples in the tutorial list is under the title "How to get your own IP address". This example creates a simple HTTP server on your phone that you can connect to from your host machine. If you have WiFi enabled on the phone and it's on the same network as your host machine, you'll be able to use a web browser to view the files on your SD card. This can come in really handy when you're out and about and you'd like to quickly move a picture from your phone to say your netbook. The source code for this looks like:

import SimpleHTTPServer

from os import chdir

chdir('/sdcard/ /dcim/Camera'')


Then you just hit the IP address of your phone from a browser and download images using a right mouse click and 'Save As'.

Most Popular LinuxPlanet Stories