如何检测系统对话框中的所有意见和文字

如何检测系统对话框中的所有意见和文字

本文介绍了如何检测系统对话框中的所有意见和文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个类似的情况:

Let's say I have a similar scenario:

我以我的TTS引擎使用的谈话的无障碍服务出现这个对话框的时候,但我却能检测到的唯一的事情是可选择的意见(那些由箭头所指)。
有什么方法来检测标题和(更重要的),在对话框里全部文本?

I'm using an accessibility service in order to my TTS engine talk when this dialog appears, but the only thing I was able to detect were the selectable views (those pointed by the arrow).Is there any way to detect the title and (more importantly) whole text inside the dialog?

推荐答案

是的。我认为这是可能的,你抓住这些项目关闭辅助活动,其中重点在单个节点上的。你想要做的,而不是什么是放眼整个视图层次结构。你可以做的两种方式这一个。首先要注意的是,辅助节点是一棵树。就像视图层次结构是一棵树。事实上,此树视图层次结构相匹配,几乎是1比1。开发人员可以强制不包括在视图层次的元素,尽管这不是在实践中经常做。即使他们这样做,你可以考虑获得这些信息。假设我们希望这些信息。我们要做的第一件事是确保它包括在内。

Yes. I think it is likely that you're grabbing these items off of accessibility events, which focus on a single node. What you want to do instead is look at the entire view hierarchy. You can do this one of two ways. First thing to note is that Accessibility Nodes are a tree. Just like the view heirarchy is a tree. In fact, this tree matches the view hierarchy, almost 1 to 1. Developers can force an element to not be included in the view hierarchy, though this isn't done often in practice. Even if they do you can get this information regardless. Let's assume we want this information. First thing we want to do is make sure it's included.

protected void onServiceConnected() {
    super.onServiceConnected();

    AccessibilityServiceInfo tempInfo = getServiceInfo();
    tempInfo.flags |= AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
    setServiceInfo(tempInfo);

}

这是极有可能,你可以跳过这一步,但以防万一!

It's highly likely you can skip this step, but just in case!

这之后,它很简单。首先,这样您就可以看到这些信息,让我们写一个可爱的小记录功能。

After this it's quite simple. First, so that you can see where this information is, let's write a cute little logging function.

public static void logNodeHeirarchy(AccessibilityNodeInfo nodeInfo, int depth) {

    if (nodeInfo == null) return;

    String logString = "";

    for (int i = 0; i < depth; ++i) {
        logString += " ";
    }

    logString += "Text: " + nodeInfo.getText() + " " + " Content-Description: " + nodeInfo.getContentDescription();

    Log.v(LOG_TAG, logString);

    for (int i = 0; i < nodeInfo.getChildCount(); ++i) {
        logNodeHeirarchy(nodeInfo.getChild(i), depth + 1);
    }
}

此功能需要进入一个辅助节点的整个树。添加它作为静态函数到你的无障碍服务。现在我们只需要调用它的根节点。您可以轻松地更改记录到日志中的属性。我发现的文字,内容描述和视图id是最有用的。

This function should log the entire tree of an accessibility node. Add it as a static function to your accessibility service. Now we just need to call it on the root node. You can easily change the properties that get logged. I find text, content description, and view id to be the most useful.

@Override
public void onAccessibilityEvent(AccessibilityEvent e) {

    switch (e.getEventType()) {
        case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {
            logNodeHeirarchy(getRootInActiveWindow(), 0);
        }
    }
}

这应该让你看到的信息。所有你需要做的是找出如何解析它。注意:您也可以从叶节点爬上,使用的getParent()。

This should allow you to see where the information is. All you have to do is figure out how to parse it. Note: You can also crawl up from a leaf node, using getParent().

这篇关于如何检测系统对话框中的所有意见和文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:36