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

PPT
Arletti & Partners Relocation
PPTX
Poch dela rosa_how to use pod-o-matic.ppt
PPSX
Quién es quién
PPT
Adding and subtracting fractions
DOCX
Self awareness
PDF
Filosofía y ciencia
PPT
Tarhana
PPTX
11.4
Arletti & Partners Relocation
Poch dela rosa_how to use pod-o-matic.ppt
Quién es quién
Adding and subtracting fractions
Self awareness
Filosofía y ciencia
Tarhana
11.4

Viewers also liked (16)

PPT
Chapter1.8
PDF
Intro to Pattern Lab
PDF
Professional learning persuasive writing
PPT
Chapter3.3
PPT
Chapter2.8
RTF
Aji amarillo
PPT
Chapter5.4
PPS
Ken's digital images
PDF
Internet of Things
ODS
цахим тест Bayarmaa
PDF
Using LinkedIn to Generate Business
ODP
Soliragchaa 3
PPTX
Walla faces dinner
PDF
Bandwidth dan throughput
PDF
Ns dsc1110 a-10-0197_v2_web_english_final_lr
PPTX
7 Email Deliverability Myths
Chapter1.8
Intro to Pattern Lab
Professional learning persuasive writing
Chapter3.3
Chapter2.8
Aji amarillo
Chapter5.4
Ken's digital images
Internet of Things
цахим тест Bayarmaa
Using LinkedIn to Generate Business
Soliragchaa 3
Walla faces dinner
Bandwidth dan throughput
Ns dsc1110 a-10-0197_v2_web_english_final_lr
7 Email Deliverability Myths
Ad

Recently uploaded (20)

PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
Architecture types and enterprise applications.pdf
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Abstractive summarization using multilingual text-to-text transfer transforme...
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
Five Habits of High-Impact Board Members
PPT
Geologic Time for studying geology for geologist
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
Configure Apache Mutual Authentication
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Microsoft Excel 365/2024 Beginner's training
Architecture types and enterprise applications.pdf
A contest of sentiment analysis: k-nearest neighbor versus neural network
Enhancing emotion recognition model for a student engagement use case through...
Abstractive summarization using multilingual text-to-text transfer transforme...
sustainability-14-14877-v2.pddhzftheheeeee
Convolutional neural network based encoder-decoder for efficient real-time ob...
Five Habits of High-Impact Board Members
Geologic Time for studying geology for geologist
Benefits of Physical activity for teenagers.pptx
1 - Historical Antecedents, Social Consideration.pdf
Configure Apache Mutual Authentication
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
A comparative study of natural language inference in Swahili using monolingua...
OpenACC and Open Hackathons Monthly Highlights July 2025
Custom Battery Pack Design Considerations for Performance and Safety
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Ad

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