如何使用 AccessibilityService 从活动窗口获取所有内容

问题描述 投票:0回答:1

更新窗口时如何获取屏幕上的所有内容?这可能吗?

意味着当我打开和关闭任何应用程序以及设备的主屏幕时,我想从活动窗口获取所有文本。

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeViewClicked|typeViewLongClicked|typeViewSelected|typeViewFocused|typeViewTextChanged|typeWindowStateChanged|typeNotificationStateChanged|typeViewHoverEnter|typeViewHoverExit|typeTouchExplorationGestureStart|typeTouchExplorationGestureEnd|typeWindowContentChanged|typeViewScrolled|typeViewTextSelectionChanged|typeAllMask"
    android:accessibilityFeedbackType="feedbackGeneric"    android:accessibilityFlags="flagDefault|flagIncludeNotImportantViews|flagReportViewIds|flagRequestFilterKeyEvents|flagRetrieveInteractiveWindows"
    android:canRequestFilterKeyEvents="false"
    android:canRetrieveWindowContent="true"
    android:canTakeScreenshot="true"
    android:description="@string/app_name"
    android:notificationTimeout="100" />
android text accessibility accessibilityservice
1个回答
0
投票

要从辅助服务中检索当前屏幕上的所有文本,您可以使用以下方法:

@Override
public void onAccessibilityEvent(AccessibilityEvent e) {
     AccessibilityNodeInfo root = getRootInActiveWindow();
     if (root != null) {
         List<String> allTexts = getAllTexts(root); // This array contains all texts present on screen
     }
}

// This traverses all the node tree and get the nodes' text recursively
public List<String> getAllTexts(AccessibilityNodeInfo node) {
    ArrayList<String> texts = new ArrayList<>();
    if (node == null) {
        return texts;
    }
    if (node.getText() != null && node.getText() != "") {
        texts.add(node.getText());
    }
    for (int i = 0; i < node.getChildCount(); i++) {
        texts.addAll(getAllTexts(node.getChild(i)));
    }
    return texts;
}
© www.soinside.com 2019 - 2024. All rights reserved.