使用可访问性服务在屏幕上查找按钮或文本的坐标

发布于 2025-02-04 19:35:33 字数 318 浏览 3 评论 0原文

是否可以使用可访问性服务来查找按钮或屏幕上的文本的坐标,然后在该按钮或文本字段上模拟点击,但不要触摸字段,就像您需要触摸Android Talkback中的字段一样阅读?

例如,是否可以使用这样的可访问性服务:

  1. 将活动的意图发送到可访问性服务,并提供一些用于不同任务的附加功能。

  2. 在接收特定额外字符串的意图时,该服务将打开开发人员选项。

  3. 无需触摸文本“运行服务”,可访问性服务应在开发人员选项中找到该文本字段(或按钮)坐标并模拟点击。

是否可以在Android上?

Is it possible to use the accessibility service to find the coordinates of a button or the text on the screen, then simulate tapping on that button or text field, but without touching the field, like you need to touch the field in Android TalkBack to hear reading?

For example, is it possible to use the accessibility service like this:

  1. Send the intent from the activity to the accessibility service with some extras for different tasks.

  2. When receiving an intent with a certain extra string, the service will open the developer options.

  3. Without touching the text "Running services", the accessibility service should find that text field (or button) coordinates in the developer options and simulate a tap on it.

Is that possible on Android or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2025-02-11 19:35:33

tl; dr;我想我已经写了服务您可能会发现有趣的

需要牢记的一些要点:

  1. 您需要启用服务并运行它。
  2. 如果Google是不打算帮助残疾人
  3. 可访问性服务只能“查看”屏幕上的内容 - recyclerview s的问题是view> View> Viewholder是作为用户滚动的不同元素重复使用。

但是,您可以在可访问性服务中执行此操作:

    // To find a node by *fully qualified id*
    rootInActiveWindow.findAccessibilityNodeInfosByViewId("id criteria")

    // To find a node by text criteria
    rootInActiveWindow.findAccessibilityNodeInfosByText("text criteria")

id标准

通过完全资格的视图ID的资源名称找到可访问性nodeinfos,其中完全合格的ID来自“ package:id/id_resource_name”。例如,如果目标应用程序的软件包为“ foo.bar”,并且ID资源名称为“ BAZ”,则完全合格的资源ID为“ foo.bar:id/baz”。

注意:此API的用法用于UI测试自动化,并且为了报告完全资格的视图ID,如果客户端必须在配置可访问性服务时,客户端必须设置cessientibilityServiceInfo#flag_report_view_ids flag。

通过文本找到可访问性nodeinfos。比赛是情况不敏感的遏制。搜索是相对于此信息的,即此信息是遍历树的根。

注意两者:

注意:如果此视图层次结构具有SurfaceView通过SurfaceView#SetChildSurfacePackage嵌入另一个视图层次结构,则有一个限制,该API将无法在嵌入式视图层次结构上找到视图的节点。这是因为视图不了解嵌入式层次结构。相反,您可以穿越所有的孩子找到节点。

要单击一个元素:

    fun click(long: Boolean = false) {
        someAccessibilityNodeInfoObject.performAction(
            if (long) ACTION_LONG_CLICK 
            else ACTION_CLICK
        )
    }

如果您真的很热衷于获取节点的屏幕坐标,则可以随时使用 getBoundsinscreen 方法,但随后您必须完成所有的数学和工作,这比需要的要困难。 ,但是我在bash adb uiautomator时做到了dump和解析XML,剥离我想要的节点的界限,只要使用uiautomator dump禁用可访问性服务。

MIDX= START_X + ((END_X - START_X) / 2)
MIDY= START_Y + ((END_Y - START_Y) / 2)

TL;DR; I think I've written a service you might find interesting

Some points to keep in mind:

  1. You need to enable the service and have it running.
  2. Google has forbidden accessibility services to be released on the Play Store if they are not intended for helping people with disabilities
  3. Accessibility services can only "see" what is on the screen currently - this a problem with RecyclerViews as the ViewHolder is reused for different elements as the user scrolls.

However you can do this in an accessibility service:

    // To find a node by *fully qualified id*
    rootInActiveWindow.findAccessibilityNodeInfosByViewId("id criteria")

    // To find a node by text criteria
    rootInActiveWindow.findAccessibilityNodeInfosByText("text criteria")

When searching by id criteria:

Finds AccessibilityNodeInfos by the fully qualified view id's resource name where a fully qualified id is of the from "package:id/id_resource_name". For example, if the target application's package is "foo.bar" and the id resource name is "baz", the fully qualified resource id is "foo.bar:id/baz".

Note: The primary usage of this API is for UI test automation and in order to report the fully qualified view id if an AccessibilityNodeInfo the client has to set the AccessibilityServiceInfo#FLAG_REPORT_VIEW_IDS flag when configuring the AccessibilityService.

When searching by text criteria:

Finds AccessibilityNodeInfos by text. The match is case insensitive containment. The search is relative to this info i.e. this info is the root of the traversed tree.

Note for both:

Note: If this view hierarchy has a SurfaceView embedding another view hierarchy via SurfaceView#setChildSurfacePackage, there is a limitation that this API won't be able to find the node for the view on the embedded view hierarchy. It's because views don't know about the embedded hierarchies. Instead, you could traverse all the children to find the node.

To click on an element:

    fun click(long: Boolean = false) {
        someAccessibilityNodeInfoObject.performAction(
            if (long) ACTION_LONG_CLICK 
            else ACTION_CLICK
        )
    }

If you are really passionate about getting the screen coordinates of a node you can always use the getBoundsInScreen method, but then you have to do all the math and stuff and that's harder than it needs to be. But I did it in bash when doing a adb uiautomator dump and parsing the XML, stripping the bounds for the node I want, just be wary that using uiautomator dump disables the accessibility service.

MIDX= START_X + ((END_X - START_X) / 2)
MIDY= START_Y + ((END_Y - START_Y) / 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文