尝试 Android Scripting Environment 之三

尝试 Android Scripting Environment 之三

作者: 宋立新

Emailzjujoe@yahoo.com

 

前言

ASE 让人爱不释手,python 也是令人发狂的好东西,所以我们继续深入学习 ASE + Python!

 

远程运行

根据参考1的信息,我们写了一个脚本,和前面的脚本(run_ase_python_scritpt.sh)一样,它可以在 Android 设备上运行位于 PC 侧的 python 脚本。

 

run_ase_python_scritpt.sh 不一样,它并不是把脚本放到目标机上再执行,而是在 PC 本地运行!

 

通过将某个 TCP 端口转发到目标机,这样:

1.         python的解释执行在 PC

2.         核心的功能调用在目标机上

 

从而效率更高了!也可见 ASE 支持的Python PC 侧没有两样!我们也从分体会到分布式计算的魅力!

 

~/android/testing/ase/python$ cat run_ase_python_scritpt2.sh

#!/bin/sh

#================================================================

# By:    zjujoe@yahoo.com

# YOU SHOULD INSTALL ASE AND PYTHON INTERPRETER ON TARGET FIRSTLY

#

# I am using ase_r25.apk, for other version, blow path may change

#

#start ASE sever  first:

# adb -s emulator-5554 shell am start -a com.google.ase.action.LAUNCH_SERVER -n com.google.ase/.activity.AseServiceLauncher

#

#then get AP_PORT number for notification

#================================================================

#set -x

#change this which ASE server listion to

TARGET_AP_PORT=40729

DEVICE="-s emulator-5554"

export AP_PORT=9999

 

#the script file to be run in ase

if [ $# -eq "1" ]; then

    FILE=$1

else

    echo $0 filename

    exit

fi

 

if ! [ -e android.py  ]; then

    echo "we need anroid.py from ase"

    exit

fi

 

adb ${DEVICE} forward tcp:${AP_PORT} tcp:${TARGET_AP_PORT}

python $1

 

注意:如果 ase 服务端口设置错误,将会出错:

songlixin@zjujoe-desktop:~/android/testing/ase/python$ ./run_ase_python_scritpt2.sh  hello_world.py

Traceback (most recent call last):

  File "hello_world.py", line 3, in <module>

    droid.makeToast('Hello, Android!')

  File "/home/songlixin/android/testing/ase/python/android.py", line 54, in rpc_call

    return self._rpc(name, *args)

  File "/home/songlixin/android/testing/ase/python/android.py", line 43, in _rpc

    response = self.client.readline()

  File "/usr/lib/python2.6/socket.py", line 406, in readline

    data = self._sock.recv(self._rbufsize)

socket.error: [Errno 104] Connection reset by peer

 

一下两个脚本的运行时间,相差很大!

$ time ./run_ase_python_scritpt.sh  hello_world.py

0 KB/s (76 bytes in 0.087s)

5 KB/s (257 bytes in 0.044s)

real    0m1.839s

user    0m0.004s

sys 0m0.016s

 

$time ./run_ase_python_scritpt2.sh  ./hello_world.py

real    0m0.217s

user    0m0.020s

sys 0m0.016s

将脚本放到目标机上

$ adb push my_script.py /sdcard/ase/scripts

 

目标机上后台执行脚本

adb shell am start -a com.google.ase.action.LAUNCH_SCRIPT -n com.google.ase/.activity.AseServiceLauncher -e com.google.ase.extra.SCRIPT_NAME hello_world.py

目标机上终端里执行脚本

adb shell am start -a com.google.ase.action.LAUNCH_TERMINAL -n com.google.ase/.activity.AseServiceLauncher -e com.google.ase.extra.SCRIPT_NAME hello_world.py

启动 ASE server

adb shell am start -a com.google.ase.action.LAUNCH_SERVER -n com.google.ase/.activity.AseServiceLauncher

 

 

参考

1. 远程运行脚本

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

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页