SL4A - Background Scripting with Python

SL4A - Background Scripting with Python


A service is a component, which runs in the background, without direct interaction with the user. It does not have any UI (user interface). The service runs in the background indefinitely even if application is destroyed.

This chapter describes the writing scripts that perform specific tasks in the background.

Background Tasks

SL4A enables a script to run in a terminal or in the background. To launch a script in the background choose the icon that looks like a cog wheel. The script runs in the background infinitely. However, one may choose to explicitly shut down such services.

Launch on Boot Scripts

These scripts are launched whenever the device boots. The Launch on Boot preferences screen is an application that lets you specify the device’s boot preferences. The utility supports execution of multiple scripts via a Master script. The Master script launches the other scripts.


A trigger defines a set of actions that must be performed in response to an action/event. Triggers may be added as well as deleted. The /sdcard/sl4a/scripts directory allows you to choose a script to run when an event is triggered. The list of options that may trigger you script includes battery, location, phone, sensors and signal strength.

Orientation-based Actions

SL4A lets us run scripts depending on the orientation and movement of the device. The startSensingTimedAPI is used to determine the orientation and movement of the device.

The following snippet uses TTS function to notify the user when the phone has been placed face down −

import android,time 
droid = android.Android() 

while True: 
   res = droid.sensorsGetLight().result 
   if res is not None and res <=10: 
      droid.ttsSpeak("Device faced down!!") 

Location-based Actions

Library, church are the locations that you may frequently visit and you definitely want to silence your phone when you are there. SL4A allows you to build scripts that will track your location and take specific actions.

Time-based / Elapsed-Time-based Triggers

These triggers are used to perform actions at a specific time of day. The 24-hour time format should be used to specify the time. Examples include a script to set alarm, reminders etc. As against a time-based trigger, an elapsed time-based trigger executes the script after an elapsed number of time. The snooze option on an alarm application is based on this concept.

SL4A - Background Scripting with Python

Frequently Asked Questions

Ans: SL4A - Utilities using Python Facades view more..
Ans: SL4A - Building GUIs view more..
Ans: SL4A - Exploring Android API view more..
Ans: SL4A - Background Scripting with Python view more..
Ans: SL4A - Packaging & Distributing view more..

Rating - NAN/5