使用AccessibilityNodeInfo导致堆栈大小8MB错误

时间:2019-01-09 01:40:04

标签: android accessibilityservice

我使用了无障碍服务在屏幕上显示了所有文本。在几分钟正常工作后,它导致ANR带有一些GC allocated...日志,几秒钟后,它因以下错误而崩溃: enter image description here

@Override
public void onAccessibilityEvent(final AccessibilityEvent event) {
    try {
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (powerManager == null || !powerManager.isInteractive()) {
            return;
        }
        if (FirebaseAuth.getInstance().getCurrentUser() == null) return;
        CharSequence eventPackageName = event.getPackageName();
        if (eventPackageName != null) {
            if (eventPackageName.equals(BuildConfig.APPLICATION_ID)) return;
            if (eventPackageName.equals(getCurrentKeyboardPackageName())) return;
            int eventType = event.getEventType();
            if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED || eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
                    AccessibilityNodeInfo mNodeInfo = event.getSource();
                    StringBuilder stringBuilder = new StringBuilder("");
                    getAllTextViews(mNodeInfo, stringBuilder);
                    String value = stringBuilder.toString().replace('\n', ' ').replaceAll(" +", " ");
            }

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

我的getAllTextViews()方法:

private void getAllTextViews(AccessibilityNodeInfo nodeInfo, StringBuilder stringBuilder) {
        if (nodeInfo == null) return;
        if (!TextUtils.isEmpty(nodeInfo.getText())) {
            stringBuilder.append(" " + nodeInfo.getText());
        }
        for (int i = 0; i < nodeInfo.getChildCount(); i++) {
            getAllTextViews(nodeInfo.getChild(i), stringBuilder);
        }
    }

任何帮助将不胜感激。非常感谢!

1 个答案:

答案 0 :(得分:0)

您需要在使用信息后使用recycle()的{​​{1}}方法。