转自: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.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. MySql比较运算符正则式匹配REGEXP的详细
  2. 记一次MySQL的优化案例
  3. 谈谈MySQL中的隐式转换
  4. 详解MySQL 8.0 之不可见索引
  5. 实例验证MySQL|update字段为相同的值是否
  6. mysql 8.0.22 安装配置方法图文教程
  7. 详解mysql 中的锁结构
  8. 详解MySQL中的数据类型和schema优化
  9. Servermanager启动连接数据库错误如何解
  10. MySQL数据中很多换行符和回车符的解决方