Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with AccessibilityNodeInfo caused stack size 8MB error

I used accessibility service to get all texts on screen. After minutes working normally, it caused ANR with some GC allocated... logs and few seconds after, it crashed with this error: 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();
    }
}

My getAllTextViews() method:

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

Any help will be appreciated. Thank a lot!

like image 867
Truong Hieu Avatar asked May 13 '26 19:05

Truong Hieu


1 Answers

What you need is to use recycle() method of AccessibilityNodeInfo after info is used.

like image 156
Rahul Sharma Avatar answered May 15 '26 08:05

Rahul Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!