相关文章推荐
冷静的口罩  ·  Webex for Government ...·  3 月前    · 
乐观的西瓜  ·  Central Nervous ...·  1 年前    · 
果断的风衣  ·  r - ggplot set ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

In my application I have an accessibility service that gets the root of the active window before performing further actions. The relevant section looks like this:

    @Override
    public final void onAccessibilityEvent(AccessibilityEvent event) {
    AccessibilityNodeInfo rootAccessibilityNode = getRootInActiveWindow();

I'm getting complaints from users about ANRs, and the stacktrace is pointing me towards the getRootInActiveWindow line.

Is it possible that getRootInActiveWindow causes ANRs and if so, what is the mechanism? Does this method need to be called in it's own thread? There's nothing in the documentation about this.

I doubt very much that this line is the direct cause of the crash, still want to see a Stack Trace. It would also be helpful to know how you launch your service (developers new to accessibility services frequently get this wrong). Assuming you've done your homework on the usual obvious answers, we're left with the following:

A: Permissions issues. If you've done your testing on rooted devices or whatever, it may be that you're users are having permissions issues. Insure that you have android:canRetrieveWindowContent="true" in your service confix XML file.

B: If you have not filtered out any accessibility events, there are events that trigger between window loads and such. It may be that your attempting to grab onto content that is not there and the system doesn't like it. This could also be hidden by doing testing on emulators, as it could be a race condition, that does not manifest itself in the slower emulated environment. This would also cause performance issues, as the node hierarchy is a reasonably expensive thing to traverse through and you wouldn't want to do so frequently. Try limiting the accessibilityEventTypes that you have your code run on.

android:canRetrieveWindowContent="true" is already in my service confix XML file & accessibilityEventTypes used by me is "typeWindowStateChanged , typeViewFocused ,typeWindowContentChanged". still getting lot of ANR due to getRootInActiveWindow. mostly in Android 8.0 – Nikhil Oct 23, 2017 at 4:23 If you have an ANR you have a stacktrace... you should ask a quesiton, and share said stracktrace. – ChrisCM Oct 23, 2017 at 15:01

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.