cosc 5/4730

of 23 /23
Cosc 5/4730 Scripting layer for Android (SL4A)

Upload: anika

Post on 22-Feb-2016




0 download


Cosc 5/4730. Scripting layer for Android (SL4A). Android scripting. SL4A brings scripting languages to the android, by allowing you edit and execute scripts and interactive interpreters directly on the device. - PowerPoint PPT Presentation


Page 1: Cosc 5/4730

Cosc 5/4730

Scripting layer for Android(SL4A)

Page 2: Cosc 5/4730

Android scripting

• SL4A brings scripting languages to the android, by allowing you edit and execute scripts and interactive interpreters directly on the device.– The scripts have access to many APIs available in

applications, but simplified interface.– They can run interactively or in the background

– A note, SL4A is still at the “alpha” level.

Page 3: Cosc 5/4730

Supported languages

• Python, Perl, JRuby, Lua, BeanShell (a Java language based scripting language), JavaScript, Tcl, and shell (borne)

• More are planned to be added.

Page 4: Cosc 5/4730


• Get the sl4a_r4.apk (or more current version) from

• Download this to your android to device, then click install

• For emulators, download (Google APIs), then issue the following command:– adb install sl4a_r4.apk

• This command can also be used to install on a your android device as well, if it is plugged via usb.

Page 5: Cosc 5/4730

Running SL4A

• You should get an icon in the application launcher:

• This is used to open the interpreter.• By default it comes with the shell scripting

language. – You can now write and run shell scripts.

Page 6: Cosc 5/4730

To install more languages

• In the interpreters screen select Add menu item, then select to language you want to install, say perl or python– It will then download and you will need to install

the language.

Page 7: Cosc 5/4730

To install more languages (2)

• You can also download the apk to the computer and install them using the adb command, example:– adb install perl_for_android_r1.apk

– Click the icon and then click install.

Page 8: Cosc 5/4730

Under the hood

From Practical Android projects, Ch5 Introducing SL4A, Apress

Page 9: Cosc 5/4730

Writing scripts.

• You can write scripts like you would on computers, to do many things.

• To access the android APIs you need the following• Perl:– Use Android– my $droid = Android->new();

• Python:– Import android– Droid = android.Android();

Page 10: Cosc 5/4730

A note• Actually write the scripts on the device can be difficult

– Using the onscreen keyboard, plus the text can be pretty small in the editor.

– Turn on the usb debugging on the device.– You can then use the following command to transfer the script

over• adb push /sdcard/sl4a/scripts

• There is also a TextEditor that maybe be very useful– Textedit-sl4a.apk

• You can also setup a remote connection via these directions–

Page 11: Cosc 5/4730

Documentation and Alpha status

• While there are a lot of examples– In python, perl, and other languages

• The documentation is pretty poor.• Also, since it is an alpha level program, things

change with little (or no notice) between versions.

Page 12: Cosc 5/4730

Hello World example • Pythonimport androiddroid = android.Android()droid.makeToast("Hello World")

• Perluse Android;my $a = Android->new();$a->makeToast("Hello, Android!");

• More “hello world” examples can be found here–

Page 13: Cosc 5/4730

“Simple” Example• The following uses the ttsSpeak to have the driod speak what you

type into a dialog box with perluse Android;my $droid = Android->new();

$tospeak = "hi!";while($tospeak ne "done") {

$result = $droid->ttsSpeak($tospeak);$hash = $droid->dialogGetInput("Enter done to exit", "Text to

Speak?");$tospeak = $hash->{'result'};


Page 15: Cosc 5/4730

GUI pieces

• Note, this code comes from the script, installed with perl. Python has a similar script.

• Toast$droid->makeToast(‘Hello, Perl?’);

• To make the phone vibrate$droid->vibrate();

Page 16: Cosc 5/4730

Alert Dialog• my $title = 'User Interface';• my $message = 'Welcome to the ASE integration test.';• $droid->dialogCreateAlert( $title, $message );• $droid->dialogSetPositiveButtonText('Continue');• $droid->dialogShow();• my $response = $droid->dialogGetResponse()->{'result'};

Page 17: Cosc 5/4730

GUI pieces$droid->dialogCreateAlert( $title, $message );$droid->dialogSetPositiveButtonText('Yes');$droid->dialogSetNegativeButtonText('No');$droid->dialogSetNeutralButtonText('Cancel');$droid->dialogShow();my $response = $droid->dialogGetResponse->{'result'};my $which = $response->{'which'};• Results use these keys 'positive', 'negative', 'neutral';

Page 18: Cosc 5/4730

Spinner Progress dialog• my $title = 'Spinner';• my $message = 'This is simple spinner progress.';• $droid->dialogCreateSpinnerProgress( $title, $message );• $droid->dialogShow();• sleep 2; #so the spinner stays up for 2 seconds.• $droid->dialogDismiss();

Page 19: Cosc 5/4730

Horizontal Progress bar$title = 'Horizontal'; $message = 'This is simple horizontal progress.';$droid->dialogCreateHorizontalProgress( $title, $message, 50 );$droid->dialogShow();for my $x ( 0 .. 50 ) { sleep 1; $droid->dialogSetCurrentProgress($x);}$droid->dialogDismiss();

Page 20: Cosc 5/4730

Dialog with a list

• my $title = 'Alert';• $droid->dialogCreateAlert($title);• $droid->dialogSetItems( [ qw/foo bar baz/ ] );• $droid->dialogShow();• my $response = $droid->dialogGetResponse()-


Page 21: Cosc 5/4730

Dialog with a choice list• my $title = 'Alert';• $droid->dialogCreateAlert($title);• $droid->dialogSetSingleChoiceItems( [ qw/foo bar baz/ ] );• $droid->dialogSetPositiveButtonText('Yay!');• $droid->dialogShow();• my $response = $droid->dialogGetResponse()->{'result'};

Page 22: Cosc 5/4730

Dialog with Multi Choice list• my $title = 'Alert';• $droid->dialogCreateAlert($title);• $droid->dialogSetMultiChoiceItems( [ qw/foo bar baz/ ], [] );• $droid->dialogSetPositiveButtonText('Yay!');• $droid->dialogShow();• my $response = $droid->dialogGetResponse()->{'result'};