.首先在Android上开启 SL4A 进入 Interpreter界面start Server.选择private usb连接,这时SL4A开启服务器,比如:127.0.0.1:40122
.在PC中将android.py (=>http://android-scripting.googlecode.com/hg/python/ase/android.py)拷贝到python安装目录或当前目录下
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
.在shell中导入环境变量 $export AP_HOST=127.0.0.1 $export AP_PORT=9999(可以写入~/.bashrc中;$source ~/.bashrc)
.$adb forward tcp:9999 tcp:40122 #(android上开的端口号)
.现在可以开启python了
boat@boat-K40AB:~/work/android/python$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import android
>>> droid = android.Android()
>>> droid.makeToast("Hello from my computer!")
Result(id=0, result=None, error=None)
posted on 2012-05-31 12:39
lixiucheng 阅读(508)
评论(0) 编辑 收藏 引用