230 likes | 325 Views
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.
E N D
Cosc5/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. • 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.
Supported languages • Python, Perl, JRuby, Lua, BeanShell (a Java language based scripting language), JavaScript, Tcl, and shell (borne) • More are planned to be added.
Installing • Get the sl4a_r4.apk (or more current version) from http://code.google.com/p/android-scripting/downloads/list • 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.
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.
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.
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.
Under the hood From Practical Android projects, Ch5 Introducing SL4A, Apress
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();
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 script.pl /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 • http://code.google.com/p/android-scripting/wiki/RemoteControl
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.
Hello World example • Python import android droid = android.Android() droid.makeToast("HelloWorld") • Perl use Android;my $a = Android->new();$a->makeToast("Hello, Android!"); • More “hello world” examples can be found here • http://code.google.com/p/android-scripting/wiki/AndroidFacadeAPI
“Simple” Example • The following uses the ttsSpeak to have the driod speak what you type into a dialog box with perl use 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'}; }
APIs. • http://code.google.com/p/android-scripting/wiki/ApiReference
GUI pieces • Note, this code comes from the test.pl script, installed with perl. Python has a similar script. • Toast $droid->makeToast(‘Hello, Perl?’); • To make the phone vibrate $droid->vibrate();
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'};
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';
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();
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();
Dialog with a list • my $title = 'Alert'; • $droid->dialogCreateAlert($title); • $droid->dialogSetItems( [ qw/foo bar baz/ ] ); • $droid->dialogShow(); • my $response = $droid->dialogGetResponse()->{'result'};
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'};
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'};
References • http://code.google.com/p/android-scripting/ • Introducing SL4A: The scripting layer for android, Pieter Greyling • http://android-scripting.googlecode.com/files/Practical_Android_Projects_Ch05_Introducing_SL4A.pdf