Tôi nên trả lời câu hỏi của mình vì tôi đã tìm ra cách tốt hơn để làm điều đó. Tôi thấy dự án này sử dụng uiautomator togheter với một máy chủ cân rpc ánh sáng, do đó bạn có thể gửi lệnh đến thiết bị:
https://github.com/xiaocong/android-uiautomator-server#build
này làm cho việc bán phá giá gần như ngay lập tức và hoạt động thực sự tốt đẹp. Ông cũng có một dự án python nếu bạn muốn xem làm thế nào để thực hiện các cuộc gọi rpc:
https://github.com/xiaocong/uiautomator
Nhưng tôi đã tạo ra một ví dụ nhỏ ở đây.
Bắt đầu máy chủ:
# Start the process
process = subprocess.Popen(
'adb shell uiautomator runtest '
'bundle.jar uiautomator-stub.jar '
'-c com.github.uiautomatorstub.Stub', stdout=subprocess.PIPE, shell=True)
# Forward adb ports
subprocess.call('adb forward tcp:9008 tcp:9009')
Chức năng gọi lệnh ("ping", "dumpWindowHierarchy" vv):
def send_command(self, method_name, *args):
"""
Send command to the RPC server
Args:
method_name(string): Name of method to run
*args: Arguments
"""
data = {
'jsonrpc': '2.0',
'method': method_name,
'id': 1,
}
if args:
data['params'] = args
request = urllib2.Request(
'http://localhost:{0}/jsonrpc/0'.format(self.local_port),
json.dumps(data),
{
'Content-type': 'application/json'
})
try:
result = urllib2.urlopen(request, timeout=30)
except Exception as e:
return None
if result is None:
return None
json_result = json.loads(result.read())
if 'error' in json_result:
raise JsonRPCError('1',
'Exception when sending command to '
'UIAutomatorServer: {0}'.format(
json_result['error']))
return json_result['result']
Lưu ý rằng bạn phải đẩy các tập tin (bundle.jar anduiautomator -stub.jar) từ dự án đầu tiên đến thiết bị trước và đặt chúng trong "/ data/local/tmp /"