`

android SL4A

阅读更多
转自:http://code.google.com/p/android-scripting/wiki/RemoteControl


ntroduction
Sometimes it's nice to be able to write scripts on your computer and run them on the phone. This is possible using SL4A's "server" support.

First, start an SL4A server from the Interpreter Manager. (From the SL4A frontpage, press Menu, then Interpreters, then Menu, then Start Server). You can either start a public or a private server (recommended).

If you start a public server, anyone that can reach your device's IP address can control your phone. Because of this, we recommend that you start a private server instead. However, to use a private server you will need to install the Android SDK and add the adb utility to your path.

When you use a private server and set up port forwarding and environment variables as described below, then you will be able to run scripts on your computer that should also work unmodified when run directly on the phone. NICE. This is not true for public servers.

To use a private server, first connect your device to your computer via USB (this is not necessary if you are using a public server).

Note the port displayed in the SL4A notification in the Android notification area,
Use adb to forward the network connection to your phone, AND
Set the AP_PORT environment variable.
Example: Suppose you've started a private server within SL4A which you've noted is listening on port 4321. You've connected the device to your computer with a USB cable, and you've verified that "USB debugging connected" shows up in the Android notification area.

You would then set up port forwarding like:

$ adb forward tcp:9999 tcp:4321
This command will forward all local traffic targeting port 9999 to the private-server port on the Android device (port 4321 in this example).

Next you must set up an environment variable (AP_PORT) that the android bindings use to find the local port. Continuing with the example above, you would do:

$ export AP_PORT=9999  # Use set AP_PORT=9999 on Windows
If you don't want to connect your device to the host over USB, you can use a public server. For public servers, you must additionally export the IP of the device. To extend the previous example, suppose your phone is connected over wifi on your home network and has an IP address of 192.168.0.100. You would now need to do:

$ export AP_PORT=4321  # Use set AP_PORT=9999 on Windows
$ export AP_HOST=192.168.0.100  # Use set AP_HOST=192.168.0.100 on Windows
Now you can execute Python or Lua on your PC, import the android package or module for the language, and interact with the AndroidProxy on the phone.

For Example
For our example, let's use Python.

First, make sure that you have Python 2.6 installed on your computer (this matches the Python version that SL4A uses on the phone).

Then, make sure you have the SL4A's android.py module in your Python path (i.e. in site-packages or the current directory). android.py is the only extra file you need; you don't need to do any sort of extensive SL4A install on your computer.

Then, connect your Android device via USB, start a private server, and set up adb port forwarding and the AP_PORT environment variable as described above.

Now you can have some fun!

$ python
Python 2.6
Type "help", "copyright", "credits" or "license" for more information.
>>> import android  # The SL4A android.py module should be on your sys.path.
>>> droid = android.Android()  # Or android.Android(('192.168.1.101', 6789)) for a public server, where 192.168.1.101 is the public server's IP, and 6789 is its port.
>>> droid.makeToast("Hello from my computer!")
Rhino Example
A HOWTO for Rhino.

Copying Scripts to Your Phone
Once you're satisfied, you can copy script to your phone:

$ adb push my_script.py /sdcard/sl4a/scripts
Now you can execute or continue to edit the script on your phone.

Remotely Starting SL4A
It is possible to launch scripts on your phone via the adb shell:

Launch a script in the background.
$ am start -a com.googlecode.android_scripting.action.LAUNCH_BACKGROUND_SCRIPT -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher -e com.googlecode.android_scripting.extra.SCRIPT_PATH /sdcard/sl4a/scripts/test.py
Launch a script in a terminal.
$ am start -a com.googlecode.android_scripting.action.LAUNCH_FOREGROUND_SCRIPT -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher -e com.googlecode.android_scripting.extra.SCRIPT_PATH /sdcard/sl4a/scripts/test.py
It is also possible to launch an SL4A server via the adb shell:

Start a private server.
$ am start -a com.googlecode.android_scripting.action.LAUNCH_SERVER -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher
Start a public server.
$ am start -a com.googlecode.android_scripting.action.LAUNCH_SERVER -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher --ez com.googlecode.android_scripting.extra.USE_PUBLIC_IP true
Troubleshooting
If you've mounted your SD card on your computer, the lists of scripts will appear empty in SL4A on your phone. Don't panic. As soon as you unmount the SD card on your computer, SL4A will display the list of scripts again.
If you try to set up adb port forwarding and get "no device found" errors, ensure that "USB debugging" is enabled on your phone and all proper drivers are installed on your computer. You can enable USB debugging on your phone under Settings > Applications > Development > USB debugging. In general, external scripting via private server will only work if "adb devices" shows your android device after you've connected it via USB. If "adb devices" comes up empty, Google around for tips on this problem before trying to move forward with SL4A itself.
分享到:
评论

相关推荐

    android sl4a

    sl4a for android script 开发

    SL4A-of-Python_API_Chinese.rar_SL4A之Python_API_python android_py

    SL4A,全称为Scripting Layer for Android,是一款强大的Android平台上的脚本开发工具,它允许用户使用多种脚本语言(如Python、JavaScript等)来控制Android设备,进行自动化任务和交互。标题中的"SL4A-of-Python_...

    PythonForAndroid-r7b1+sl4a.zip

    PythonForAndroid-r7b1+sl4a.zip这个压缩包文件是关于在Android平台上运行Python环境的工具集合,其中包含了PythonForAndroid和SL4A(Scripting Layer for Android)两个重要组件。这两个工具使得开发者可以在...

    Android脚本语言环境SL4A.zip

    Google官方博客介绍了Android Scripting Environment(ASE、SL4A),将脚本语言带入Android,允许用户编辑和执行脚本,直接在Android设备上运行交互式解释器。脚本将能大幅度简化任务界面,用户能在交互式终端中使用...

    SL4A之Python_API_中英文参考

    SL4A,全称为Scripting Layer for Android,是一款强大的Android平台上的自动化工具,它允许用户通过编程语言(如Python)来控制和与Android设备交互。这个压缩包包含了SL4A的Python API的中英文参考文档,是开发者...

    sl4a_r4.apk

    SL4A,全称为"Scripting Layer for Android",是一个为Android平台设计的开源项目,旨在让开发者和用户能够通过脚本语言控制Android设备。SL4A提供了丰富的API,允许编写简单的脚本来实现对Android设备的各种操作,...

    Apress.Pro.Android.Python.with.SL4A.Jul.2011.rar

    《Apress.Pro.Android.Python.with.SL4A.Jul.2011》是一部关于在Android平台上使用Python编程的专业书籍,结合了SL4A(Scripting Layer for Android)这一强大的工具,让开发者能够利用Python语言的简洁性和强大功能...

    sl4a-r6.1.1-arm-debug

    SL4A,全称为"Scripting Layer for Android",是一个为Android平台设计的开源项目,旨在让开发者和普通用户能够通过脚本语言控制和自动化Android设备。SL4A提供了丰富的API,可以调用Android系统的各种功能,如...

    SL4A 的一本教程

    SL4A(Scripting Layer for Android)是为Android平台设计的一套脚本层,它允许开发者使用各种脚本语言,比如Python、Ruby、Lua等,来编写Android应用程序而不需要使用Java。SL4A的主要目标是提供一种简便快速的方式...

    PythonForAndroid+SL4A

    PythonForAndroid、SL4A两个安装包,安装后可在安卓平台上运行python

    Pro.Android.Python.with.SL4A(第1版)

    《Pro.Android.Python.with.SL4A(第1版)》是一本专注于使用Python语言以及少量JavaScript在Android平台上开发实际应用的书籍。本书的核心在于介绍Scripting Layer for Android(SL4A)项目,一个旨在满足特定需求的...

    sl4a-r6x05-armv7-debug

    SL4A,全称为"Scripting Layer for Android",是一个为Android平台设计的脚本环境,它允许用户通过各种编程语言编写控制Android设备的应用程序。SL4A的主要目的是简化Android应用的开发,使得非专业程序员也能创建...

    Android代码-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 ...

    SL4A开发工具包

    SL4A开发APK文档集合 pro-android-python-with-sl4a.pdf PythonForAndroid_r4.apk sl4a_r6.apk [android.开发书籍].Practical.Android.Projects.pdf

    安卓SL4A_R6.APK文件

    android脚本环境的APK应用,目前已支持Python,Lua, BeanShell, Perl等语言。

    sl4a-r3.rar

    SL4A,全称为Scripting Layer for Android,是一款在Android平台上运行脚本语言的框架,旨在让开发者能够方便地在Android设备上编写和执行各种脚本。标题中的"sl4a-r3.rar"指的是SL4A的第三版(r3)的压缩包文件,这...

    PythonForAndroid-r1-sl4a-r6.rar

    PythonForAndroid、SL4A两个安装包,安装后可在安卓平台上运行python。

    Pro_Android_Scripting_with_SL4A__Writing_Android_Native_Apps_Using_Python__Lua__and_Beanshell.mobi

    Pro_Android_Scripting_with_SL4A__Writing_Android_Native_Apps_Using_Python__Lua__and_Beanshell.mobi

    sl4a_r6.apk

    sl4a_r6 供自己学习,也分享出来给大家使用,需要的话可以下载,有什么问题可以留言

    sl4a-r6.1.0-armv7-debug

    SL4A,全称为"Scripting Layer for Android",是一个为Android平台设计的脚本环境,它允许用户通过各种编程语言来控制和自动化Android设备。SL4A的主要目的是简化Android设备的编程,使得非专业开发者也能轻松进行...

Global site tag (gtag.js) - Google Analytics