尝试 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

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值