在Android中获取活动中所有窗口的视图层次结构

6

我正在制作自动化SKD。因此,我的需求与普通应用程序开发略有不同。

要求: 获取当前活动的View层次结构。 问题: 当Spinner未打开时,我可以正确获取它。但是,在其打开时我无法获取Spinner的详细信息。

我使用以下代码来获取层次结构。问题是: Spinner是否托管在不同的窗口中,这就是为什么我找不到它的原因?该如何解决这个问题呢?

    //This is how I start recursion to get view hierarchy
    View view = getWindow().getDecorView().getRootView();
    if (view instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) view;
        dumpViewHierarchyWithProperties( group, 0);
    }

//Functions to get hierarchy
private  void dumpViewHierarchyWithProperties(ViewGroup group,int level) {
    if (!dumpViewWithProperties(group, level)) {
        return;
    }

    final int count = group.getChildCount();
    for (int i = 0; i < count; i++) {
        final View view = group.getChildAt(i);
        if (view instanceof ViewGroup) {
            dumpViewHierarchyWithProperties((ViewGroup) view, level + 1);
        } else {
            dumpViewWithProperties(view, level + 1);
        }
    }
}

private  boolean dumpViewWithProperties(View view,int level) {

    //Add to view Hierarchy.
    return true;
}

你成功获取了打开的Spinner视图吗?如果是,能否分享一下如何实现的呢? - Emil Adz
1个回答

1
我已经成功获取了打开的Spinner弹出视图层次结构,使用了你代码的一部分以及其他反射技巧,这是完整的代码,请注意,反射会影响SDK和使用它的应用程序的性能。
//Function to get all available windows of the application using reflection
private void logRootViews() {
    try {
        Class wmgClass = Class.forName("android.view.WindowManagerGlobal");
        Object wmgInstnace = wmgClass.getMethod("getInstance").invoke(null, (Object[])null);

        Method getViewRootNames = wmgClass.getMethod("getViewRootNames");
        Method getRootView = wmgClass.getMethod("getRootView", String.class);
        String[] rootViewNames = (String[])getViewRootNames.invoke(wmgInstnace, (Object[])null);

        for(String viewName : rootViewNames) {
            View rootView = (View)getRootView.invoke(wmgInstnace, viewName);
            Log.i(TAG, "Found root view: " + viewName + ": " + rootView);
            getViewHierarchy(rootView);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//Functions to get hierarchy
private void getViewHierarchy(View view) {
    //This is how I start recursion to get view hierarchy
    if (view instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) view;
        dumpViewHierarchyWithProperties(group, 0);
    } else {
        dumpViewWithProperties(view, 0);
    }
}

private void dumpViewHierarchyWithProperties(ViewGroup group, int level) {
    if (!dumpViewWithProperties(group, level)) {
        return;
    }

    final int count = group.getChildCount();
    for (int i = 0; i < count; i++) {
        final View view = group.getChildAt(i);
        if (view instanceof ViewGroup) {
            dumpViewHierarchyWithProperties((ViewGroup) view, level + 1);
        } else {
            dumpViewWithProperties(view, level + 1);
        }
    }
}

private boolean dumpViewWithProperties(View view, int level) {
    //Add to view Hierarchy.
    if (view instanceof TextView) {
        Log.d(TAG, "TextView from hierarchy dumped: " + view.toString() + " with text: " + ((TextView) view).getText().toString() + " ,in Level: " + level);
    } else {
        Log.d(TAG, "View from hierarchy dumped: " + view.toString() + " ,in Level: " + level);
    }
    return true;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接