My favorites | Sign in
Logo
                
Search
for
Updated Sep 29, 2009 by damonkohler
Labels: Featured
LuaAndroidAPI  
A description of the Android API available to Lua scripts.

Having trouble? Got questions? Check the FAQ or try the ASE discussion group.

Introduction

Note: the Lua and Python APIs are purposefully very similar!

ASE allows Lua scripts to interact with the system over JSON RPC calls (see the Javadoc for the AndroidProxy class for details). This is made mostly transparent using the "android" Lua package. A few example scripts are installed with ASE. These instructions are primarily example based and assume that you are familiar with the Lua scripting language. You can try out these snippets in an interactive ASE Lua terminal.

To start, every Lua script that interacts with the available Android APIs will require the android package.

require "android"

Conveniently, any scripts you write are also available for import from other scripts.

Messages and Notifications

android.makeToast("Hello, Android!")

Hello, and you are?

name = android.getInput("Hello!", "What is your name?")
android.printDict(name)  -- A convenience method for inspecting dicts (tables).
android.makeToast("Hello, " .. name.result)

Reading and Modifying Settings

volume = android.getRingerVolume()
android.setRingerVolume(5)
android.setRingerSilent(true)
android.vibrate(100)  -- 100 milliseconds of vibration.
android.setWifiEnabled(false)

Reading Sensors

The snippet below will read sensors and print out all available data from them. The example script shutup.lua reads sensors to enable silent mode when the phone is placed face down.

android.startSensing()
android.sleep(1)  --Give the sensors a moment to come online.
sensors = android.readSensors()
android.printDict(sensors.result)

Finding Your Location

Reading location information is much the same as reading sensor data.

android.startLocating()
android.sleep(1)
location = android.readLocation()
android.printDict(location.result)

Or, this is even easier (though not always up to date).

location = android.getLastKnownLocation()
android.printDict(location.result)

Text to Speech

ASE supports TTS via the Eyes-Free project. You'll need to install TTS service from the App Market in order to use it.

android.speak("I can talk!")

Sending SMS Messages

android.sendTextMessage("8675309", "Hey, Jenny!")

Raising Common Intents

Convenience methods provide access to many commonly used intents.

android.dialNumber("8675309")
android.callNumber("8675309")
android.map("pizza")  -- or "munich" or "1600 amphitheatre pkwy"
android.showContacts()
android.email()

Raising Arbitrary Intents

While many intents have convenient wrappers, you can also raise any URI based intent directly.

android.startActivity('android.intent.action.CALL', uri)

Starting an Activity for Result

Starting an activity for result allows you to do things like scanning barcodes. Again, there are several convenience methods in addition to the ability to raise arbitrary intents.

code = android.scanBarcode()
android.printDict(code.result)
pic = android.captureImage()
android.printDict(code.result)
contact = android.pickContact()
android.call(contact.result.data)

Exiting the Activity or Service

By default, the service or activity will keep running even after the script exits. To cause the activity to exit, call exit.

android.exit()

It is also possible to set a result intent before exiting.

android.setResultExtra('some_string', 'string_value')
android.setResultExtra('some_int', 42)
android.setResultExtra('some_double', 3.14159)
android.exitWithResultOk()
-- or...
android.exitWithResultCanceled()

Sign in to add a comment