1 / 23

Cosc 5/4730

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.

anika
Télécharger la présentation

Cosc 5/4730

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cosc5/4730 Scripting layer for Android (SL4A)

  2. 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.

  3. Supported languages • Python, Perl, JRuby, Lua, BeanShell (a Java language based scripting language), JavaScript, Tcl, and shell (borne) • More are planned to be added.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. Under the hood From Practical Android projects, Ch5 Introducing SL4A, Apress

  9. 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();

  10. 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

  11. 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.

  12. 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

  13. “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'}; }

  14. APIs. • http://code.google.com/p/android-scripting/wiki/ApiReference

  15. 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();

  16. 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'};

  17. 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';

  18. 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();

  19. 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();

  20. Dialog with a list • my $title = 'Alert'; • $droid->dialogCreateAlert($title); • $droid->dialogSetItems( [ qw/foo bar baz/ ] ); • $droid->dialogShow(); • my $response = $droid->dialogGetResponse()->{'result'};

  21. 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'};

  22. 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'};

  23. 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

More Related