title: Getting lot of ANR using Accessibility service in android
body: <p>i am getting ANR from reports</p>
  
  <blockquote>
    <p>Broadcast of Intent { act=android.intent.action.PACKAGE_ADDED
    dat=package:com.google.android.apps.maps flg=0x4000010
    cmp=xx.xx.xx/com.microsoft.aad.adal.ApplicationReceiver (has extras) }</p>
    
    <p>Broadcast of Intent { act=android.intent.action.PACKAGE_ADDED
    dat=package:com.google.android.apps.translate flg=0x4000010
    cmp=xx.xx.xx/com.microsoft.aad.adal.ApplicationReceiver (has extras) }</p>
    
    <p>"main" prio=5 tid=1 TimedWaiting   | group="main" sCount=1 dsCount=0
    obj=0x7527b598 self=0xe9105400   | sysTid=29196 nice=0 cgrp=default
    sched=0/0 handle=0xebfd2534   | state=S schedstat=( 0 0 0 ) utm=1236
    stm=469 core=2 HZ=100   | stack=0xff733000-0xff735000 stackSize=8MB<br>
    | held mutexes=   at java.lang.Object.wait! (Native method)
    - waiting on &lt;0x0fa0a2fe> (a java.lang.Object)   at java.lang.Object.wait (Object.java:407)   at
    android.view.accessibility.AccessibilityInteractionClient.waitForResultTimedLocked
    (AccessibilityInteractionClient.java:685)   at
    android.view.accessibility.AccessibilityInteractionClient.getFindAccessibilityNodeInfosResultAndClear
    (AccessibilityInteractionClient.java:580)
    - locked &lt;0x0fa0a2fe> (a java.lang.Object)   at android.view.accessibility.AccessibilityInteractionClient.findAccessibilityNodeInfoByAccessibilityId
    (AccessibilityInteractionClient.java:292)   at
    android.view.accessibility.AccessibilityNodeInfo.getChild
    (AccessibilityNodeInfo.java:851)   at xxx.traverseNode
    (FooAccessibilityService.java:85)   at xxx.traverseNode
    (FooAccessibilityService.java:86)   at xxx.traverseNode
    (FooAccessibilityService.java:86)   at xxx.traverseNode
    (FooAccessibilityService.java:86)   at xxx.collectNodes
    (FooAccessibilityService.java:66)   at xxx.onAccessibilityEvent
    (FooAccessibilityService.java:365)   at
    android.accessibilityservice.AccessibilityService$2.onAccessibilityEvent
    (AccessibilityService.java:1449)   at
    android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage
    (AccessibilityService.java:1585)   at
    com.android.internal.os.HandlerCaller$MyHandler.handleMessage
    (HandlerCaller.java:37)   at android.os.Handler.dispatchMessage
    (Handler.java:102)   at android.os.Looper.loop (Looper.java:154)   at
    android.app.ActivityThread.main (ActivityThread.java:6316)   at
    java.lang.reflect.Method.invoke! (Native method)   at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
    (ZygoteInit.java:872)   at com.android.internal.os.ZygoteInit.main
    (ZygoteInit.java:762)</p>
  </blockquote>
  
  <p>My code is </p>
  
  <pre><code>    ArrayList&lt;AccessibilityNodeInfo&gt; collectNodes(AccessibilityNodeInfo node) {
  
      ArrayList&lt;AccessibilityNodeInfo&gt; nodeInfoArrayList = new ArrayList&lt;&gt;();
      try {
          int childCount = node.getChildCount();
          for (int index = 0; index &lt; childCount; index++) {
              AccessibilityNodeInfo childNode = node.getChild(index);
  
  
              traverseNode(childNode);
              if (childNodes != null &amp;&amp; childNodes.size() &gt; 0) {
                  nodeInfoArrayList.addAll(childNodes);
                  childNodes.clear();
              }
          }
      }
      catch (Exception e){
          e.printStackTrace();
      }
      return nodeInfoArrayList;
  }
  
  private void traverseNode(AccessibilityNodeInfo node) {
      try {
          AccessibilityNodeInfo edittextNode = null;
          if (null == node)
              return;
  
          final int count = node.getChildCount();
          if (count &gt; 0) {
              for (int i = 0; i &lt; count; i++) {
                  AccessibilityNodeInfo childNode = node.getChild(i);
                  if (childNode == null) {
                      node.recycle();
                      return;
                  }
                  else {
                      traverseNode(childNode);
                  }
              }
          } else {
  
          }
      }
      catch (Exception error){
          error.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Any idea how to resolve this issue</p>