gpt4 book ai didi

java - 由于 AccessibilityNodeInfo getChild 而导致大量 ANR

转载 作者:太空狗 更新时间:2023-10-29 13:51:18 34 4
gpt4 key购买 nike

收到大量 ANR 报告,

"main" prio=5 tid=1 Runnable | group="main" sCount=0 dsCount=0 flags=0 obj=0x72e8a568 self=0xe65da000 | sysTid=20592 nice=0 cgrp=default sched=0/0 handle=0xe9f6b4a8 | state=R schedstat=( 35792446568 9751828904 97371 ) utm=2328 stm=1249 core=2 HZ=100 | stack=0xff6e5000-0xff6e7000 stackSize=8MB | held mutexes= "mutator lock"(shared held) native: pc 00000000002c45b7 /system/lib/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+130) native: pc 0000000000355a83 /system/lib/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMapb+202) native: pc 0000000000351f67 /system/lib/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMapb+34) native: pc 00000000003698df /system/lib/libart.so (_ZN3art14DumpCheckpoint3RunEPNS_6ThreadE+654) native: pc 000000000035662b /system/lib/libart.so (_ZN3art6Thread21RunCheckpointFunctionEv+298) native: pc 00000000003bbcff /system/lib/libart.so (_ZN3art16JniMethodFastEndEjPNS_6ThreadE+46) native: pc 0000000000403fdb /system/framework/arm/boot-framework.oat (Java_android_os_Parcel_nativeReadInt__J+114) at android.os.Parcel.nativeReadInt (Native method) at android.os.Parcel.readInt (Parcel.java:1966) at android.os.Parcel.readExceptionCode (Parcel.java:1906) at android.os.Parcel.readException (Parcel.java:1885) at android.accessibilityservice.IAccessibilityServiceConnection$Stub$Proxy.findAccessibilityNodeInfoByAccessibilityId (IAccessibilityServiceConnection.java:447) at android.view.accessibility.AccessibilityInteractionClient.findAccessibilityNodeInfoByAccessibilityId (AccessibilityInteractionClient.java:286) at android.view.accessibility.AccessibilityNodeInfo.getChild (AccessibilityNodeInfo.java:959) at MyAccessibilityService.traverseNode (MyAccessibilityService.java:94) at MyAccessibilityService.traverseNode (MyAccessibilityService.java:100) at MyAccessibilityService.traverseNode (MyAccessibilityService.java:100) at MyAccessibilityService.traverseNode (MyAccessibilityService.java:100) at MyAccessibilityService.collectTextNodes (MyAccessibilityService.java:69) at MyAccessibilityService.onAccessibilityEvent (MyAccessibilityService.java:384) at android.accessibilityservice.AccessibilityService$2.onAccessibilityEvent (AccessibilityService.java:1527) at android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage (AccessibilityService.java:1712) at com.android.internal.os.HandlerCaller$MyHandler.handleMessage (HandlerCaller.java:37)

    "main" prio=5 tid=1 Native
| group="main" sCount=1 dsCount=0 flags=1 obj=0x72e8a568 self=0xe65da000
| sysTid=11518 nice=0 cgrp=default sched=0/0 handle=0xe9f6b4a8
| state=S schedstat=( 34572861602 18459176949 199877 ) utm=2350 stm=1106 core=1 HZ=100
| stack=0xff6e5000-0xff6e7000 stackSize=8MB
| held mutexes=
native: pc 0000000000018dac /system/lib/libc.so (syscall+28)
native: pc 00000000000b3729 /system/lib/libart.so (_ZN3art17ConditionVariable16WaitHoldingLocksEPNS_6ThreadE+88)
native: pc 00000000003bbbe3 /system/lib/libart.so (_ZN3artL12GoToRunnableEPNS_6ThreadE+306)
native: pc 00000000003bba81 /system/lib/libart.so (_ZN3art12JniMethodEndEjPNS_6ThreadE+8)
native: pc 00000000007ca401 /system/framework/arm/boot-framework.oat (Java_android_os_BinderProxy_transactNative__ILandroid_os_Parcel_2Landroid_os_Parcel_2I+144)
at android.os.BinderProxy.transactNative (Native method)
at android.os.BinderProxy.transact (Binder.java:748)
at android.accessibilityservice.IAccessibilityServiceConnection$Stub$Proxy.findAccessibilityNodeInfoByAccessibilityId (IAccessibilityServiceConnection.java:446)
at android.view.accessibility.AccessibilityInteractionClient.findAccessibilityNodeInfoByAccessibilityId (AccessibilityInteractionClient.java:286)
at android.view.accessibility.AccessibilityNodeInfo.getChild (AccessibilityNodeInfo.java:959)
at MyAccessibilityService.traverseNode (MyAccessibilityService.java:94)
at MyAccessibilityService.traverseNode (MyAccessibilityService.java:100)
at MyAccessibilityService.traverseNode (MyAccessibilityService.java:100)
at MyAccessibilityService.traverseNode (MyAccessibilityService.java:100)
at MyAccessibilityService.collectTextNodes (MyAccessibilityService.java:69)
at MyAccessibilityService.onAccessibilityEvent (MyAccessibilityService.java:384)
at android.accessibilityservice.AccessibilityService$2.onAccessibilityEvent (AccessibilityService.java:1527)
at android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage (AccessibilityService.java:1712)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage (HandlerCaller.java:37)
at android.os.Handler.dispatchMessage (Handler.java:105)


"main" prio=5 tid=1 Runnable
| group="main" sCount=0 dsCount=0 flags=0 obj=0x73bf7568 self=0xe7445000
| sysTid=18941 nice=0 cgrp=default sched=0/0 handle=0xeae2e4a8
| state=R schedstat=( 169234820933 190791369297 635928 ) utm=11752 stm=5170 core=0 HZ=100
| stack=0xff795000-0xff797000 stackSize=8MB
| held mutexes= "mutator lock"(shared held)
at java.util.ArrayList.clear (ArrayList.java:565)
at android.view.accessibility.AccessibilityNodeInfo.init (AccessibilityNodeInfo.java:3211)
at android.view.accessibility.AccessibilityNodeInfo.obtain (AccessibilityNodeInfo.java:3015)
at android.view.accessibility.AccessibilityCache.getNode (AccessibilityCache.java:231)
- locked <0x0f41ae94> (a java.lang.Object)
at android.view.accessibility.AccessibilityInteractionClient.findAccessibilityNodeInfoByAccessibilityId (AccessibilityInteractionClient.java:272)
at android.view.accessibility.AccessibilityNodeInfo.getChild (AccessibilityNodeInfo.java:959)
at MyAccessibilityService.traverseNode (MyAccessibilityService.java:84)
at MyAccessibilityService.traverseNode (MyAccessibilityService.java:85)
at MyAccessibilityService.traverseNode (MyAccessibilityService.java:85)
at MyAccessibilityService.traverseNode (MyAccessibilityService.java:85)
at MyAccessibilityService.collectTextNodes (MyAccessibilityService.java:66)
at MyAccessibilityService.onAccessibilityEvent (MyAccessibilityService.java:360)
at android.accessibilityservice.AccessibilityService$2.onAccessibilityEvent (AccessibilityService.java:1527)
at android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage (AccessibilityService.java:1712)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage (HandlerCaller.java:37)
at android.os.Handler.dispatchMessage (Handler.java:105)

"main" prio=5 tid=1 TimedWaiting
| group="main" sCount=1 dsCount=0 flags=1 obj=0x73228568 self=0xed55a000
| sysTid=19996 nice=0 cgrp=default sched=0/0 handle=0xed8414a8
| state=S schedstat=( 39022541552 53623486893 130046 ) utm=3252 stm=648 core=2 HZ=100
| stack=0xff636000-0xff638000 stackSize=8MB
| held mutexes=
at java.lang.Object.wait (Native method)
- waiting on <0x0b44b730> (a java.lang.Object)
at java.lang.Object.wait (Object.java:422)
at android.view.accessibility.AccessibilityInteractionClient.waitForResultTimedLocked (AccessibilityInteractionClient.java:687)
at android.view.accessibility.AccessibilityInteractionClient.getFindAccessibilityNodeInfosResultAndClear (AccessibilityInteractionClient.java:582)
- locked <0x0b44b730> (a java.lang.Object)
at android.view.accessibility.AccessibilityInteractionClient.findAccessibilityNodeInfoByAccessibilityId (AccessibilityInteractionClient.java:291)
at android.view.accessibility.AccessibilityInteractionClient.getRootInActiveWindow (AccessibilityInteractionClient.java:160)
at android.accessibilityservice.AccessibilityService.getRootInActiveWindow (AccessibilityService.java:572)
at MyAccessibilityService.onAccessibilityEvent (MyAccessibilityService.java:370)

我的代码

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

Android 8.0 中的更多问题。知道如何处理这些 ANR。

配置文件

<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"

android:accessibilityEventTypes="typeWindowStateChanged|typeViewFocused|typeWindowContentChanged"

android:notificationTimeout="100"
android:canRetrieveWindowContent="true"

android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagReportViewIds|flagRequestEnhancedWebAccessibility"
android:description="Test"/>

请注意几乎所有来自 Android 8.0 设备的 ANR 报告。

最佳答案

  1. 首要的事情是回收所有由getChildfindAccessibilityNodeInfoByAccessibilityId 或任何其他函数分配的节点。所以你应该有这样的东西:
AccessibilityNodeInfo findFirstViewByText(AccessibilityNodeInfo rootNode, String text) {
if (rootNode == null) return null;
if (rootNode.getText() != null && rootNode.getText().toString().equals(text))
return rootNode;
int childCount = rootNode.getChildCount();
for (int i = 0; i < childCount; i++) {
AccessibilityNodeInfo tmpNode = rootNode.getChild(i);
if (tmpNode != null) {
AccessibilityNodeInfo res = findFirstViewByText(tmpNode, text);
if (res != null) {
return res;
} else {
tmpNode.recycle();
}
}
}
return null;
}
}

node = findFirstViewByText(rootNode, "something");
if(node != null) {
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
node.recycle();
}
  1. 请记住,当您与目标应用程序交互时,onAccessibilityEvent 可能会被过于频繁地调用,而且您在此函数中的处理会阻塞目标应用程序上的 UI 线程。您可以通过以下方式克服此问题:

    • 限制对 onAccessibilityEvent 的调用,即如果最后一次执行发生在 x 毫秒前,则不要执行您的代码,但要确保最后一次调用会触发代码的执行。
    • 通过避免不必要的遍历来提高代码的性能。定义状态,以便应用仅查找与当前状态相关的节点。
  2. 随时分享您的更多代码,以便我/我们能够了解您的目标,这样我们就可以提供更多帮助。正如我从您的日志中看到的那样,有几个线程遍历无障碍服务的虚拟 dom,它们导致了死锁。

关于java - 由于 AccessibilityNodeInfo getChild 而导致大量 ANR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46789325/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com