http://android-scripting.googlecode.com/hg/python/ase/android.py
拷贝到/Python27/Lib/site-packages这个目录下
Code:
1 # Copyright (C) 2009 Google Inc. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 # use this file except in compliance with the License. You may obtain a copy of 5 # the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 # License for the specific language governing permissions and limitations under 13 # the License. 14 15 __author__ = 'Damon Kohler <damonkohler@gmail.com>' 16 17 import collections 18 import json 19 import os 20 import socket 21 import sys 22 23 PORT = os.environ.get('AP_PORT') 24 HOST = os.environ.get('AP_HOST') 25 HANDSHAKE = os.environ.get('AP_HANDSHAKE') 26 Result = collections.namedtuple('Result', 'id,result,error') 27 28 29 class Android(object): 30 31 def __init__(self, addr=None): 32 if addr is None: 33 addr = HOST, PORT 34 self.conn = socket.create_connection(addr) 35 self.client = self.conn.makefile() 36 self.id = 0 37 if HANDSHAKE is not None: 38 self._authenticate(HANDSHAKE) 39 40 def _rpc(self, method, *args): 41 data = {'id': self.id, 42 'method': method, 43 'params': args} 44 request = json.dumps(data) 45 self.client.write(request+'\n') 46 self.client.flush() 47 response = self.client.readline() 48 self.id += 1 49 result = json.loads(response) 50 if result['error'] is not None: 51 print result['error'] 52 # namedtuple doesn't work with unicode keys. 53 return Result(id=result['id'], result=result['result'], 54 error=result['error'], ) 55 56 def __getattr__(self, name): 57 def rpc_call(*args): 58 return self._rpc(name, *args) 59 return rpc_call
EG.
1 import android 2 app = android.Android(('192.168.1.100',52583)) 3 msg = "Hello world! Remote debug." 4 app.makeToast(msg)
Result(id=0, result=None, error=None)
1 #导入Android的API模块 2 import android 3 #获得操作对象 4 droid = android.Android(('192.168.1.100', 52583)) 5 #运行扫描程序,返回一个元组 6 code = droid.scanBarcode() 7 #从扫描程序返回的元组中取得isbn编号 8 isbn = code[1]['extras']['SCAN_RESULT'] 9 #构造查询书籍的Url 10 url = 'http://book.douban.com/subject_search?search_text=%s&cat=1001' % isbn 11 #打开浏览器,传入构造好的Url,返回查找结果 12 droid.startActivity('android.intent.action.VIEW',url)