android – 任何更快的转储UI层次结构的方法?

现在我正在使用uiautomator像这样转储UI:
adb shell uiautomator dump

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

解决方法

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

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

这使得倾销几乎立即,并且工作得非常好.如果你想看看如何进行rpc调用,他还有一个python项目:

https://github.com/xiaocong/uiautomator

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

启动服务器:

# 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”等):

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 anduiautomator-stub.jar)从第一个项目推送到设备,然后将它们放在“/ data / local / tmp /”中

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...