SlideShare a Scribd company logo
1 of 31
Download to read offline
Outline                    Introduction                 Usage                   Examples
                            ..                          ...                     ...
                                                        ....                    .
                                                        .........               ...
                                                        ....




                          INTRODUCTION TO SL4A

                                 louieli yafeilee@qq.com


                                    September 4, 2012




                                                        .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                      Introduction   Usage                   Examples
                              ..            ...                     ...
                                            ....                    .
                                            .........               ...
                                            ....




          1 Outline

          2 Introduction
                Overview of the SL4A
          3 Usage
                Install
                Architecture
                usage
                Sharing Scripts
          4 Examples
                download
                httpserver
                text to speech
                                            .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                     Introduction              Usage                   Examples
                             ..                       ...                     ...
                                                      ....                    .
                                                      .........               ...
                                                      ....
Overview of the SL4A




          Scripting Layer for Android (SL4A) brings scripting languages to
          Android by allowing you to edit and execute scripts and interactive
          interpreters directly on the Android device. These scripts have
          access to many of the APIs available to full-fledged Android
          applications, but with a greatly simplified interface that makes it
          easy to get things done.




                                                      .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                   Introduction   Usage                   Examples
                           ..            ...                     ...
                                         ....                    .
                                         .........               ...
                                         ....
Overview of the SL4A




                                         .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                     Introduction             Usage                   Examples
                             ..                      ...                     ...
                                                     ....                    .
                                                     .........               ...
                                                     ....
Install




          http://blog.csdn.net/louieuser/article/details/7912661
          http://blog.csdn.net/louieuser/article/details/7916280




                                                      .    .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                   Introduction   Usage                   Examples
                           ..            ...                     ...
                                         ....                    .
                                         .........               ...
                                         ....
Install




                                         .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                   Introduction   Usage                   Examples
                           ..            ...                     ...
                                         ....                    .
                                         .........               ...
                                         ....
Install




                                         .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                   Introduction   Usage                   Examples
                           ..            ...                     ...
                                         ....                    .
                                         .........               ...
                                         ....
Architecture




                                         .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                   Introduction   Usage                   Examples
                           ..            ...                     ...
                                         ....                    .
                                         .........               ...
                                         ....
Architecture




                                         .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                   Introduction   Usage                   Examples
                           ..            ...                     ...
                                         ....                    .
                                         .........               ...
                                         ....
Architecture




                                         .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                      Introduction                Usage                     Examples
                              ..                         ...                       ...
                                                         ....                      .
                                                         .........                 ...
                                                         ....
Architecture




          All SL4A API calls return an object with three fields:
               id: a strictly increasing, numeric id associated with the API
               call.
               result: the return value of the API call, or null if there is no
               return value.
               error: a description of any error that occurred or null if no
               error occurred.




                                                         .     .     .   .     .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                    Introduction           Usage                   Examples
                            ..                    ...                     ...
                                                  ....                    .
                                                  .........               ...
                                                  ....
usage


Python



  1       import android
  2

  3       droid = android.Android()
  4       name = droid.getInput(”Hello!”, ”What is your name?”)
  5       print name # name is a namedtuple
  6       droid.makeToast(”Hello, %s” % name.result)




                                                  .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                       Introduction        Usage                   Examples
                               ..                 ...                     ...
                                                  ....                    .
                                                  .........               ...
                                                  ....
usage


Lua



  1       require ”android”
  2

  3       name = android.getInput(”Hello!”, ”What is your name?”)
  4       android.printDict(name)
  5       android.makeToast(”Hello, ” .. name.result)




                                                  .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                   Introduction        Usage                   Examples
                           ..                 ...                     ...
                                              ....                    .
                                              .........               ...
                                              ....
usage


Perl




  1       use Android;
  2       my $a = Android−>new();
  3       $a−>makeToast(”Hello, Android!”);




                                              .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                    Introduction             Usage                   Examples
                            ..                      ...                     ...
                                                    ....                    .
                                                    .........               ...
                                                    ....
usage


JavaScript




  1       load(”/sdcard/ase/extras/rhino/android.js”);
  2       var droid = new Android();
  3       droid.makeToast(”Hello, Android!”);




                                                     .    .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                    Introduction       Usage                   Examples
                            ..                ...                     ...
                                              ....                    .
                                              .........               ...
                                              ....
usage


JRuby




  1       require ”android”
  2       droid = Android.new
  3       droid.makeToast ”Hello, Android!”




                                              .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                    Introduction            Usage                   Examples
                            ..                     ...                     ...
                                                   ....                    .
                                                   .........               ...
                                                   ....
usage


BeanShell




  1       source(”/sdcard/ase/extras/bsh/android.bsh”);
  2       droid = Android();
  3       droid.call(”makeToast”, ”Hello, Android!”);




                                                    .     .    .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                    Introduction           Usage                    Examples
                            ..                    ...                      ...
                                                  ....                     .
                                                  .........                ...
                                                  ....
usage


Tcl




  1       package require android
  2       set android [android new]
  3       set name [$android getInput ”Hello!” ”What is your name?”]
  4       $android makeToast ”Hello, $name”




                                                   .    .     .   .    .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                    Introduction            Usage                     Examples
                            ..                     ...                       ...
                                                   ....                      .
                                                   .........                 ...
                                                   ....
usage




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




                                                    .    .     .   .     .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                           Introduction            Usage                     Examples
                                   ..                     ...                       ...
                                                          ....                      .
                                                          .........                 ...
                                                          ....
usage

  1       #!/usr/bin/env python
  2       #Launch app.py
  3       import subprocess
  4       ADB = r’d:Androidandroid−sdkplatform−toolsadb.exe’
  5       APPLICATION = ’speakit.py’
  6       TARGET = ’/sdcard/sl4a/scripts/’
  7
  8       def main():
  9           # Upload the application.
 10           subprocess.call([ADB, ’−e’, ’push’, APPLICATION, TARGET + APPLICATION])
 11           # Launch the application.
 12           subprocess.call(’”%s” −e shell am start 
 13           −a com.googlecode.android scripting.action.LAUNCH BACKGROUND SCRIPT 
 14           −n com.googlecode.android scripting/.activity.ScriptingLayerServiceLauncher 
 15           −e com.googlecode.android scripting.extra.SCRIPT PATH 
 16           ”%s%s”’ % (ADB, TARGET, APPLICATION))
 17
 18       if   name       == ’ main ’:
  19            main()                                     .    .     .   .     .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                        Introduction               Usage                   Examples
                                ..                        ...                     ...
                                                          ....                    .
                                                          .........               ...
                                                          ....
Sharing Scripts


QR Code


           1      Open http://zxing.appspot.com/generator/
           2      Open the Contents drop down and choose Text.
           3      On the first line of the Text Content, enter the name of the
                  script.
           4      Below that, paste the script content.
           5      Open the Size drop down and choose L.
           6      Click Generate.
           7      Embed or share the resulting barcode image with your friends.

                                                          .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                   Introduction   Usage                   Examples
                           ..            ...                     ...
                                         ....                    .
                                         .........               ...
                                         ....
Sharing Scripts


QR Code




                                         .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                        Introduction                 Usage                   Examples
                                ..                          ...                     ...
                                                            ....                    .
                                                            .........               ...
                                                            ....
Sharing Scripts


QR Code


          To download the script to your phone:
            1     Launch SL4A or return the scripts list.
            2     Press the Menu button.
            3     Tap Add.
            4     Tap Scan Barcode.
            5     Scan the barcode and SL4A will add the script to your list.
          A QR code can encode 4,296 characters of content. So, this is
          only effective for small scripts.


                                                            .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                    Introduction            Usage                   Examples
                            ..                     ...                     ...
                                                   ....                    .
                                                   .........               ...
                                                   ....
Sharing Scripts


Scripts as APKs




          https://code.google.com/p/android-
          scripting/wiki/SharingScripts#Scripts as APKs




                                                    .     .    .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                        Introduction                 Usage                    Examples
                                ..                          ...                      ...
                                                            ....                     .
                                                            .........                ...
                                                            ....
download




  1       def reporthook(numblocks,blocksize,filesize,url=None):
  2           base = os.path.basename(url)
  3           try:
  4                percent = min((numblocks ∗ blocksize ∗ 100) / filesize, 100)
  5           except:
  6                percent = 100
  7           if numblocks != 0:
  8                droid.dialogSetMaxProgress(filesize)
  9                droid.dialogSetCurrentProgress(numblocks ∗ blocksize)




                                                             .    .     .    .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                        Introduction                 Usage                   Examples
                                ..                          ...                     ...
                                                            ....                    .
                                                            .........               ...
                                                            ....
download



  1       def main():
  2         global droid
  3         droid = android.Android()
  4         url = ’http://code.jquery.com/jquery−1.8.1.js’
  5         if url is None: return
  6         dst = droid.dialogGetInput(’Filename’,’Save file as:’,
  7            os.path.basename(url)).result
  8         droid.dialogCreateHorizontalProgress(’Downloading...’,
  9            ’Saving %s from web.’ % dst)
 10         droid.dialogShow()
 11         urllib.urlretrieve(url, downloads + dst,
 12            lambda nb, bs, fs, url=url: reporthook(nb,bs,fs,url))
 13         droid.dialogDismiss()
 14         droid.dialogCreateAlert(’Operation Finished’,
 15            ’%s has been saved to %s.’ % (url,downloads + dst))
 16         droid.dialogShow()

                                                             .     .    .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                   Introduction   Usage                   Examples
                           ..            ...                     ...
                                         ....                    .
                                         .........               ...
                                         ....
download




                                         .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                        Introduction                   Usage                   Examples
                                ..                            ...                     ...
                                                              ....                    .
                                                              .........               ...
                                                              ....
httpserver




  1       import android, socket, SimpleHTTPServer, struct
  2       from os import chdir
  3
  4       droid = android.Android()
  5
  6       ipdec = droid.wifiGetConnectionInfo().result[’ip address’]
  7       ipstr = socket.inet ntoa(struct.pack(’L’,ipdec))
  8
  9       chdir(’/sdcard/DCIM/’)
 10
 11       print ”connect to %s” % ipstr
 12       SimpleHTTPServer.test()




                                                              .       .   .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                       Introduction                Usage                      Examples
                               ..                         ...                        ...
                                                          ....                       .
                                                          .........                  ...
                                                          ....
text to speech


  1       <html>
  2        <head>
  3          <title>Text to Speech</title>
  4          <script>
  5            var droid = new Android();
  6            var speak = function() {
  7              droid.eventPost(”say”, document.getElementById(”say”).value);
  8            }
  9         </script>
 10        </head>
 11        <body>
 12          <form onsubmit=”speak(); return false;”>
 13            <label for=”say”>What would you like to say?</label>
 14            <input type=”text” id=”say” />
 15            <input type=”submit” value=”Speak” />
 16          </form>
 17        </body>
 18       </html>
                                                           .    .     .   .      .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                       Introduction                  Usage                   Examples
                               ..                           ...                     ...
                                                            ....                    .
                                                            .........               ...
                                                            ....
text to speech




  1       import android
  2       droid = android.Android()
  3
  4       droid.webViewShow(’file:///sdcard/sl4a/scripts/text to speech.html’)
  5       while True:
  6           result = droid.eventWaitFor(’say’).result
  7           droid.ttsSpeak(result[’data’])
  8           print result
  9           break




                                                            .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A
Outline                   Introduction   Usage                   Examples
                           ..            ...                     ...
                                         ....                    .
                                         .........               ...
                                         ....
text to speech




                                         .     .     .   .   .      .

louieli yafeilee@qq.com
INTRODUCTION TO SL4A

More Related Content

Viewers also liked

Chapter1.8
Chapter1.8Chapter1.8
Chapter1.8nglaze10
 
Intro to Pattern Lab
Intro to Pattern LabIntro to Pattern Lab
Intro to Pattern LabPaul Stonier
 
Professional learning persuasive writing
Professional learning persuasive writingProfessional learning persuasive writing
Professional learning persuasive writingG.j. Darma
 
Chapter3.3
Chapter3.3Chapter3.3
Chapter3.3nglaze10
 
Chapter2.8
Chapter2.8Chapter2.8
Chapter2.8nglaze10
 
Chapter5.4
Chapter5.4Chapter5.4
Chapter5.4nglaze10
 
Ken's digital images
Ken's digital imagesKen's digital images
Ken's digital imagesKen Woolford
 
цахим тест Bayarmaa
цахим тест  Bayarmaa цахим тест  Bayarmaa
цахим тест Bayarmaa Zaya80
 
Using LinkedIn to Generate Business
Using LinkedIn to Generate BusinessUsing LinkedIn to Generate Business
Using LinkedIn to Generate BusinessPaul Stonier
 
Soliragchaa 3
Soliragchaa 3Soliragchaa 3
Soliragchaa 3Soko_92
 
Walla faces dinner
Walla faces dinnerWalla faces dinner
Walla faces dinnerdebhig
 
Bandwidth dan throughput
Bandwidth dan throughputBandwidth dan throughput
Bandwidth dan throughputslamet01
 
Ns dsc1110 a-10-0197_v2_web_english_final_lr
Ns dsc1110 a-10-0197_v2_web_english_final_lrNs dsc1110 a-10-0197_v2_web_english_final_lr
Ns dsc1110 a-10-0197_v2_web_english_final_lremteemt
 
7 Email Deliverability Myths
7 Email Deliverability Myths7 Email Deliverability Myths
7 Email Deliverability MythsEmail Delivered
 

Viewers also liked (16)

Chapter1.8
Chapter1.8Chapter1.8
Chapter1.8
 
Intro to Pattern Lab
Intro to Pattern LabIntro to Pattern Lab
Intro to Pattern Lab
 
Professional learning persuasive writing
Professional learning persuasive writingProfessional learning persuasive writing
Professional learning persuasive writing
 
Chapter3.3
Chapter3.3Chapter3.3
Chapter3.3
 
Chapter2.8
Chapter2.8Chapter2.8
Chapter2.8
 
Aji amarillo
Aji amarilloAji amarillo
Aji amarillo
 
Chapter5.4
Chapter5.4Chapter5.4
Chapter5.4
 
Ken's digital images
Ken's digital imagesKen's digital images
Ken's digital images
 
Internet of Things
Internet of ThingsInternet of Things
Internet of Things
 
цахим тест Bayarmaa
цахим тест  Bayarmaa цахим тест  Bayarmaa
цахим тест Bayarmaa
 
Using LinkedIn to Generate Business
Using LinkedIn to Generate BusinessUsing LinkedIn to Generate Business
Using LinkedIn to Generate Business
 
Soliragchaa 3
Soliragchaa 3Soliragchaa 3
Soliragchaa 3
 
Walla faces dinner
Walla faces dinnerWalla faces dinner
Walla faces dinner
 
Bandwidth dan throughput
Bandwidth dan throughputBandwidth dan throughput
Bandwidth dan throughput
 
Ns dsc1110 a-10-0197_v2_web_english_final_lr
Ns dsc1110 a-10-0197_v2_web_english_final_lrNs dsc1110 a-10-0197_v2_web_english_final_lr
Ns dsc1110 a-10-0197_v2_web_english_final_lr
 
7 Email Deliverability Myths
7 Email Deliverability Myths7 Email Deliverability Myths
7 Email Deliverability Myths
 

Recently uploaded

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 

Recently uploaded (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 

Introduction to sl4 a

  • 1. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... INTRODUCTION TO SL4A louieli yafeilee@qq.com September 4, 2012 . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 2. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... 1 Outline 2 Introduction Overview of the SL4A 3 Usage Install Architecture usage Sharing Scripts 4 Examples download httpserver text to speech . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 3. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Overview of the SL4A Scripting Layer for Android (SL4A) brings scripting languages to Android by allowing you to edit and execute scripts and interactive interpreters directly on the Android device. These scripts have access to many of the APIs available to full-fledged Android applications, but with a greatly simplified interface that makes it easy to get things done. . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 4. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Overview of the SL4A . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 5. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Install http://blog.csdn.net/louieuser/article/details/7912661 http://blog.csdn.net/louieuser/article/details/7916280 . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 6. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Install . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 7. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Install . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 8. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Architecture . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 9. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Architecture . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 10. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Architecture . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 11. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Architecture All SL4A API calls return an object with three fields: id: a strictly increasing, numeric id associated with the API call. result: the return value of the API call, or null if there is no return value. error: a description of any error that occurred or null if no error occurred. . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 12. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... usage Python 1 import android 2 3 droid = android.Android() 4 name = droid.getInput(”Hello!”, ”What is your name?”) 5 print name # name is a namedtuple 6 droid.makeToast(”Hello, %s” % name.result) . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 13. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... usage Lua 1 require ”android” 2 3 name = android.getInput(”Hello!”, ”What is your name?”) 4 android.printDict(name) 5 android.makeToast(”Hello, ” .. name.result) . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 14. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... usage Perl 1 use Android; 2 my $a = Android−>new(); 3 $a−>makeToast(”Hello, Android!”); . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 15. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... usage JavaScript 1 load(”/sdcard/ase/extras/rhino/android.js”); 2 var droid = new Android(); 3 droid.makeToast(”Hello, Android!”); . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 16. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... usage JRuby 1 require ”android” 2 droid = Android.new 3 droid.makeToast ”Hello, Android!” . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 17. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... usage BeanShell 1 source(”/sdcard/ase/extras/bsh/android.bsh”); 2 droid = Android(); 3 droid.call(”makeToast”, ”Hello, Android!”); . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 18. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... usage Tcl 1 package require android 2 set android [android new] 3 set name [$android getInput ”Hello!” ”What is your name?”] 4 $android makeToast ”Hello, $name” . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 19. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... usage http://code.google.com/p/android-scripting/wiki/ApiReference . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 20. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... usage 1 #!/usr/bin/env python 2 #Launch app.py 3 import subprocess 4 ADB = r’d:Androidandroid−sdkplatform−toolsadb.exe’ 5 APPLICATION = ’speakit.py’ 6 TARGET = ’/sdcard/sl4a/scripts/’ 7 8 def main(): 9 # Upload the application. 10 subprocess.call([ADB, ’−e’, ’push’, APPLICATION, TARGET + APPLICATION]) 11 # Launch the application. 12 subprocess.call(’”%s” −e shell am start 13 −a com.googlecode.android scripting.action.LAUNCH BACKGROUND SCRIPT 14 −n com.googlecode.android scripting/.activity.ScriptingLayerServiceLauncher 15 −e com.googlecode.android scripting.extra.SCRIPT PATH 16 ”%s%s”’ % (ADB, TARGET, APPLICATION)) 17 18 if name == ’ main ’: 19 main() . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 21. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Sharing Scripts QR Code 1 Open http://zxing.appspot.com/generator/ 2 Open the Contents drop down and choose Text. 3 On the first line of the Text Content, enter the name of the script. 4 Below that, paste the script content. 5 Open the Size drop down and choose L. 6 Click Generate. 7 Embed or share the resulting barcode image with your friends. . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 22. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Sharing Scripts QR Code . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 23. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Sharing Scripts QR Code To download the script to your phone: 1 Launch SL4A or return the scripts list. 2 Press the Menu button. 3 Tap Add. 4 Tap Scan Barcode. 5 Scan the barcode and SL4A will add the script to your list. A QR code can encode 4,296 characters of content. So, this is only effective for small scripts. . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 24. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... Sharing Scripts Scripts as APKs https://code.google.com/p/android- scripting/wiki/SharingScripts#Scripts as APKs . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 25. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... download 1 def reporthook(numblocks,blocksize,filesize,url=None): 2 base = os.path.basename(url) 3 try: 4 percent = min((numblocks ∗ blocksize ∗ 100) / filesize, 100) 5 except: 6 percent = 100 7 if numblocks != 0: 8 droid.dialogSetMaxProgress(filesize) 9 droid.dialogSetCurrentProgress(numblocks ∗ blocksize) . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 26. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... download 1 def main(): 2 global droid 3 droid = android.Android() 4 url = ’http://code.jquery.com/jquery−1.8.1.js’ 5 if url is None: return 6 dst = droid.dialogGetInput(’Filename’,’Save file as:’, 7 os.path.basename(url)).result 8 droid.dialogCreateHorizontalProgress(’Downloading...’, 9 ’Saving %s from web.’ % dst) 10 droid.dialogShow() 11 urllib.urlretrieve(url, downloads + dst, 12 lambda nb, bs, fs, url=url: reporthook(nb,bs,fs,url)) 13 droid.dialogDismiss() 14 droid.dialogCreateAlert(’Operation Finished’, 15 ’%s has been saved to %s.’ % (url,downloads + dst)) 16 droid.dialogShow() . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 27. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... download . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 28. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... httpserver 1 import android, socket, SimpleHTTPServer, struct 2 from os import chdir 3 4 droid = android.Android() 5 6 ipdec = droid.wifiGetConnectionInfo().result[’ip address’] 7 ipstr = socket.inet ntoa(struct.pack(’L’,ipdec)) 8 9 chdir(’/sdcard/DCIM/’) 10 11 print ”connect to %s” % ipstr 12 SimpleHTTPServer.test() . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 29. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... text to speech 1 <html> 2 <head> 3 <title>Text to Speech</title> 4 <script> 5 var droid = new Android(); 6 var speak = function() { 7 droid.eventPost(”say”, document.getElementById(”say”).value); 8 } 9 </script> 10 </head> 11 <body> 12 <form onsubmit=”speak(); return false;”> 13 <label for=”say”>What would you like to say?</label> 14 <input type=”text” id=”say” /> 15 <input type=”submit” value=”Speak” /> 16 </form> 17 </body> 18 </html> . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 30. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... text to speech 1 import android 2 droid = android.Android() 3 4 droid.webViewShow(’file:///sdcard/sl4a/scripts/text to speech.html’) 5 while True: 6 result = droid.eventWaitFor(’say’).result 7 droid.ttsSpeak(result[’data’]) 8 print result 9 break . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A
  • 31. Outline Introduction Usage Examples .. ... ... .... . ......... ... .... text to speech . . . . . . louieli yafeilee@qq.com INTRODUCTION TO SL4A