有更快的方法来转储UI层次结构吗

有更快的方法来转储UI层次结构吗

本文介绍了有更快的方法来转储UI层次结构吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

现在我正在使用uiautomator来转储用户界面,如下所示:

Right now I'm using uiautomator do dump the UI like this:

adb shell uiautomator dump

它工作正常,但执行该过程大约需要3秒钟.所以我想知道是否有更快的方法?就像创建转储UI的服务一样,还是需要花费同样长的时间?

And it works fine except that it takes around 3 seconds to perform it. So I wonder if there is a faster way to do it? Like creating a service that dump the UI or will it take just as long?

推荐答案

猜我应该回答自己的问题,因为我找到了一种更好的方法.我发现这个项目将uiautomator togheter与重量轻的rpc服务器配合使用,因此您可以将命令发送到设备:

Guess I should answer my own question as I found a better way to do it. I found this project that use uiautomator togheter with a light weight rpc server so you can send commands to the device:

https://github.com/xiaocong/android-uiautomator-server#build

这几乎可以立即进行倾销,并且效果非常好.如果您想了解如何进行rpc调用,他也有一个python项目:

This make the dumping almost instantly and works really nice. He also have a python project if you want to see how to make the rpc calls:

https://github.com/xiaocong/uiautomator

但是我在这里创建了一个小例子.

But I have created a small example here.

启动服务器:

# 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')

调用命令的功能("ping","dumpWindowHierarchy"等):

Function to call commands ("ping", "dumpWindowHierarchy" etc):

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']

请注意,您必须先将文件(bundle.jar和uiautomator-stub.jar)从第一个项目推送到设备,然后将其放入"/data/local/tmp/"

Note that you have to push the files (bundle.jar anduiautomator-stub.jar) from the first project to the device first and put them in "/data/local/tmp/"

这篇关于有更快的方法来转储UI层次结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:06