我有一个从Assist API遍历所有ViewNode的方法:

@Override
public void onHandleAssist(Bundle data, AssistStructure structure, AssistContent content) {
    super.onHandleAssist(data, structure, content);
    mContent = "";
    mDescription = "";
    String structuredData = content.getStructuredData();
    if (!TextUtils.isEmpty(structuredData)) {
        structuredData = structuredData.replace("\\", "");
        mContent += structuredData + " ";
    }
    Bundle extra = content.getExtras();
    ClipData clipData = content.getClipData();
    Intent intent = content.getIntent();
    Bundle intentExtra = intent.getExtras();
    Uri uri = content.getWebUri();
    if (uri != null) {
        mContent += uri.toString() + " ";
    }
    ComponentName componentName = structure.getActivityComponent();
    int nodeCount = structure.getWindowNodeCount();
    for (int i = 0; i < nodeCount; i++) {
        AssistStructure.WindowNode windowNode = structure.getWindowNodeAt(i);
        getAllContentText(windowNode.getRootViewNode(), 5);
    }
    initView();
}

private void getAllContentText(AssistStructure.ViewNode node, int levelMax) {

    if (node != null) {
        int nodeCount = node.getChildCount();
        if (!TextUtils.isEmpty(node.getText())) {
            mContent += node.getText().toString() + " ";
        }

        if (!TextUtils.isEmpty(node.getContentDescription())) {
            mDescription += node.getContentDescription() + " ";
        }
        for (int i = 0; i < nodeCount; i++) {
            AssistStructure.ViewNode childNode = node.getChildAt(i);
            getAllContentText(childNode, levelMax);
        }
    }
}


但是,如果文本是椭圆形的,我如何获得全部文本?或者,如果文本是链接文本,我可以获取与其关联的网址吗?

最佳答案

我相信您不能直接引用该应用程序的视图,您从AssistStructure获得的全部就是AssistStructure.ViewNode。视图节点的getText()方法将可见文本返回给用户。

也许如果您找到某种通过VoiceInteractionSession getContext()方法引用Window对象的方法。您可以将上下文转换为Activity或Window,然后调用getDecorView(),然后遍历子视图以从TextViews中获取全文。

关于android - 从Android Assist API检索所有文本内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37295155/

10-12 04:11
查看更多