在 android 中使用辅助功能服务获得大量 ANR

Getting lot of ANR using Accessibility service in android

我从报告中收到 ANR

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) }

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) }

"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
| held mutexes= at java.lang.Object.wait! (Native method) - waiting on <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 <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.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)

我的密码是

    ArrayList<AccessibilityNodeInfo> collectNodes(AccessibilityNodeInfo node) {

    ArrayList<AccessibilityNodeInfo> nodeInfoArrayList = new ArrayList<>();
    try {
        int childCount = node.getChildCount();
        for (int index = 0; index < childCount; index++) {
            AccessibilityNodeInfo childNode = node.getChild(index);


            traverseNode(childNode);
            if (childNodes != null && childNodes.size() > 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 > 0) {
            for (int i = 0; i < count; i++) {
                AccessibilityNodeInfo childNode = node.getChild(i);
                if (childNode == null) {
                    node.recycle();
                    return;
                }
                else {
                    traverseNode(childNode);
                }
            }
        } else {

        }
    }
    catch (Exception error){
        error.printStackTrace();
    }
}

知道如何解决这个问题

我遇到了一个非常相似的问题:

"main" prio=5 tid=1 TimedWaiting
  | group="main" sCount=1 dsCount=0 obj=0x74e352e8 self=0xb45f6a00
  | sysTid=1785 nice=0 cgrp=default sched=0/0 handle=0xb6efdde4
  | state=S schedstat=( 20808880338 15171670769 80754 ) utm=1292 stm=788 core=1 HZ=100
  | stack=0xbe022000-0xbe024000 stackSize=8MB
  | held mutexes=

  at java.lang.Object.wait! (Native method)
- waiting on <0x022019b2> (a java.lang.Object)

  at java.lang.Object.wait (Object.java:423)

  at android.view.accessibility.AccessibilityInteractionClient.waitForResultTimedLocked (AccessibilityInteractionClient.java:695)

  at android.view.accessibility.AccessibilityInteractionClient.getFindAccessibilityNodeInfosResultAndClear (AccessibilityInteractionClient.java:590)
- locked <0x022019b2> (a java.lang.Object)

  at android.view.accessibility.AccessibilityInteractionClient.findAccessibilityNodeInfosByViewId (AccessibilityInteractionClient.java:349)

  at android.view.accessibility.AccessibilityNodeInfo.findAccessibilityNodeInfosByViewId (AccessibilityNodeInfo.java:1374)

  at com.myApp.myFct (mySource.java:69)

此错误在 Android 6 和 7.1 上报告,但主要在 android 7.0

更新:

  • 我们在 android 8 上还没有很多用户,但我在 android 8 上也看到了问题。
  • 我仍然不知道如何解决这个问题。

我发现了问题,实际上这是由于 onedrive-sdk-android

根据 onedrive,我使用以下代码

repository {
    jcenter()
}

dependency {
    // Include the sdk as a dependency
    compile ('com.onedrive.sdk:onedrive-sdk-android:1.3+') {
        transitive = false
    }

    // Include the gson dependency
    compile ('com.google.code.gson:gson:2.3.1')

    // Include supported authentication methods for your application
    compile ('com.microsoft.services.msa:msa-auth:0.8.+')
    compile ('com.microsoft.aad:adal:1.1.+')
}

行以下

compile ('com.microsoft.aad:adal:1.1.+')

创建我发现的错误 here,更改后 compile ('com.microsoft.aad:adal:1.1.+') compile ('com.microsoft.aad:adal:1.13.1') 已解决问题。

如果应用程序不响应对 AccessibilityNodeInfo 的请求,来自 AccessibilityService 的请求将超时并且 return 为空。该超时设置为 5 秒。

如果您从无响应的应用中重复请求,您可能会等待很长时间。

在所示代码的第一部分中,即使失败,循环也会继续尝试遍历节点。这可能会导致很长时间的延迟。