Chinaunix首页 | 论坛 | 博客
  • 博客访问: 400391
  • 博文数量: 101
  • 博客积分: 2247
  • 博客等级: 大尉
  • 技术积分: 979
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-15 22:39
文章分类

全部博文(101)

文章存档

2012年(1)

2011年(100)

分类: 嵌入式

2011-06-19 16:04:53

前言
研究了一下 ASE ( Android Scripting Environment: ), 这个很火的项目( /topic/407925 )目前还处于 Alpha Release 状态。

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

目前封装的 API 包括 拨号 / 短信 /wifi/bt/barcode , 等等,具体见: wiki/ApiReference

========================================================================
首先从: downloads/list 下载到最新版本的 ASE, 比如 ASE_r25.apk

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

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

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

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

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

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

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

所以,我们:

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

wget

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 侧直接执行,从而解决了自动化的问题。

=====================================================================
转载:
http://blog.csdn.net/zjujoe/archive/2010/06/09/5657868.aspx
阅读(1214) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~