`
mars5337
  • 浏览: 87068 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Android Scripting Environment -ASE

阅读更多
http://code.google.com/p/android-scripting/
Android脚本环境 ( ASE )为Android引入了脚本语言支持,从而使您编辑、执行脚本,并与Android设备上的脚本解释器进行互动成为可能。这些脚本可以借用很多现有的API快速开发Android应用,并极大地简化了接口,可以轻易完成以下工作:

处理intent
启动activity
拨打电话
发送短信
扫描条形码
获取位置和传感器数据
使用文字朗读引擎( TTS )
以及更多

脚本可以在终端内交互运行,作为一个长期运行的服务启动,或由位置启动 。目前已经支持Python,Lua和BeanShell,我们正在计划将Ruby和JavaScript的支持也添加进来。




脚本可以在手机上直接进行编辑。




脚本管理器显示现有脚本。




脚本可以交互启动或作为背景服务。





交互式终端可以用来解释执行脚本





脚本可以使用Android的用户界面获取用户输入。

你可能会问,为什么使用脚本而不是开发真正的Android应用呢?诚然,Android的开发环境非常易于使用,但是你必须在电脑前完成所有工作。而ASE则使您在任何您需要的情况下,快速的在设备上使用高级脚本语言尝试您的想法。请看如下的Lua示例脚本:

--Placing the phone face down will disable the ringer. Turning it face up again will enable
--the ringer.
require "android"
android.startSensing()
android.sleep(1)  --Give the sensors a moment to come online.
silent = false
while true do
  s = android.readSensors()
  facedown = s.result and s.result.zforce and s.result.zforce > 9
  if facedown and not silent then
    android.vibrate()  --A short vibration to indicate we're in silent mode.
    android.setRingerSilent(true)
    silent = true
  elseif not facedown and silent then
    android.setRingerSilent(false)
    silent = false
  end
  android.sleep(1)
end

接下来是另一个不错的脚本,以Python语言编写:

"""Say chat messages aloud as they are received."""

import android, xmpp

_SERVER = 'talk.google.com', 5223

class SayChat(object):
  def __init__(self):
    self.droid = android.Android()
    username = self.droid.getInput('Username')['result']
    password = self.droid.getInput('Password')['result']
    jid = xmpp.protocol.JID(username)
    self.client = xmpp.Client(jid.getDomain(), debug=[])
    self.client.connect(server=_SERVER)
    self.client.RegisterHandler('message', self.message_cb)
    if not self.client:
      print 'Connection failed!'
      return
    auth = self.client.auth(jid.getNode(), password, 'botty')
    if not auth:
      print 'Authentication failed!'
      return
    self.client.sendInitPresence()

  def message_cb(self, session, message):
    jid = xmpp.protocol.JID(message.getFrom())
    username = jid.getNode()
    text = message.getBody()
    self.droid.speak('%s says %s' % (username, text))

  def run(self):
    try:
      while True:
        self.client.Process(1)
    except KeyboardInterrupt:
      pass

saychat = SayChat()
saychat.run()

这些脚本表明一些现有的API可直接用于Lua和Python。其目的是启用一个服务,当手机正面朝下放置时禁用铃声。对于某些诸如BeanShell这样的脚本语言,则可以直接访问Android的Java API 。出于简化的目的,ASE提供了AndroidFacade类。而对其它象Python和Lua这样的语言来说,API是通过使用JSON RPC调用在代理上暴露出来的。当然,这意味着只有部分被AndroidFacade和AndroidProxy封装了的那部分API才能被Python和Lua的交叉编译解释器所使用。值得庆幸的是,AndroidFacade和AndroidProxy类都很容易扩展。

如果您想试试ASE,你暂时还没法在软件商店发现它,但很快就可以见到了。目前,您可以从我们的项目主页下载最新的APK。那里还有一些示例脚本和文档为您开始使用提供帮助。我们总是乐意倾听你的想法,因此请向我们发送反馈或在ASE讨论组进行询问。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics