尝试 Android Scripting Environment

转自:http://blog.csdn.net/zjujoe/article/details/5657868

前言

研究了一下 ASE ( Android Scripting Environment: http://code.google.com/p/android-scripting/), 这个很火的项目( http://www.javaeye.com /topic/407925 )目前还处于 Alpha Release 状态。

 

它封装了一些 Java API, 从而可以通过脚本 (Python,LUA 等)去访问 某些 Java API, 从而大大提供开发效率。

 

目前封装的 API 包括 拨号 / 短信 /wifi/bt/barcode , 等等,具体见: http://code.google.com/p/android-scripting/wiki/ApiReference

 

       首先从: http://code.google.com/p/android-scripting/downloads/list 下载到最新版本的 ASE, 比如ASE_r25.apk

 

       启动 Android 设备或者虚拟设备, 通过 adb install 将 ASE 安装到目标机上。

 

在目标机上运行 ASE, 首先通过 Menu->Add Interpreter 安装一种解释器,比如 Python.

 

安装完成后会自动下载相关脚本,并切换到脚本试图。

现在我们可以上下选择并运行某个脚本。

 

修改代码

下面我们尝试修改代码。由于直接在目标机上编辑比较麻烦,我们先在 PC 侧编辑好后再用 ADB shell 将其push 到目标机上。

 

从 Logcat 信息我们可以看到 ASE 会下载脚本包:

http://android-scripting.googlecode.com/files/python_scripts_r7.zip

       并解压缩到: /sdcard/ase/scripts

 

所以,我们:

1)  使用 wget 下载相关脚本:

wget http://android-scripting.googlecode.com/files/python_scripts_r7.zip

 

2)  解开这个包。修改代码:

unzip python_scripts_r7.zip

cp hello_world.py  hello_world2.py

vi hello_world2.py

修改一下打印语句,比如 droid.makeToast('Goodbye, Android!')

 

3)  将这个修改的文件放到目标机上:

adb push hello_world2.py  /sdcard/ase/scripts

 

4)  在目标机上运行 ASE ,可以看到我们修改的脚本,并可以运行!

 

通过 adb shell 执行脚本

通过 ASE 执行 脚本比较慢,需要人工操作! 我们尝试在 PC 侧使用脚本通过 adb 来直接运行!

 

看 ASE 执行脚本的过程,似乎也只是调出了命令行,设置了一些环境变量,   最终运行脚本。

 

所以我们模仿它写了一个脚本, 如下脚本以待执行的 python 脚本为参数,将其通过 adb 放到 目标机上执行

 

#!/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

# You should change AP_PORT with the one from ASE console

#

#start ASE sever  first:

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

#       -n com.google.ase/.activity.AseServiceLauncher

#

#the get AP_PORT number for notification

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

#set -x

#change this which ASE server listion to

AP_PORT="48620"

DEVICE="-s emulator-5554"

 

#the script file to be run in ase

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

    FILE=$1

else

    echo $0 filename

    exit

fi

 

#shell script file to be run on target(which will run ase)

FILE2=t1.sh

TARGET_RUN_LOCATION=/data

TARGET_PYTHON_SCRIPTS_LOCATION=/sdcard/ase/scripts

 

 

#prepare the FILE2

echo export PYTHONPATH="/sdcard/ase/extras/python:/sdcard/ase/scripts/" > ${FILE2}

echo export AP_PORT=${AP_PORT} >> ${FILE2}

echo export TEMP="/sdcard/ase/extras/pythontmp" >> ${FILE2}

echo export PYTHONHOME="/data/data/com.google.ase/python" >> ${FILE2}

echo /data/data/com.google.ase/python/bin/python /sdcard/ase/scripts/${FILE} >> ${FILE2}

 

#push FILE to target

adb ${DEVICE} push ${FILE} ${TARGET_PYTHON_SCRIPTS_LOCATION}

 

#push FILE2 to target

adb ${DEVICE} push ${FILE2} ${TARGET_RUN_LOCATION}

adb ${DEVICE} shell chmod 777 ${TARGET_RUN_LOCATION}/${FILE2}

 

#run FILE2

adb ${DEVICE} shell ${TARGET_RUN_LOCATION}/${FILE2}

 

#remove FILE2 from target & local

adb ${DEVICE} shell rm ${TARGET_RUN_LOCATION}/${FILE2}

rm ${FILE2}

 

#remove FILE from target

#adb ${DEVICE} shell rm ${FILE} ${TARGET_PYTHON_SCRIPTS_LOCATION}

                                                       

如上脚本可以很好的在 PC 侧直接执行,从而解决了自动化的问题。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值