2

我正在使用 AccessibilityService 来确定在 android 设备中运行的任何当前应用程序的视图的文本和坐标。在每个事件 onAccessibilityEvent(...) 运行,但我不知道如何控制这些事件。我使用 event.getSource() 来获取每个窗口内容更改的结果。我的 AccessibilityService 的 xml 设置是:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeWindowContentChanged|typeViewFocused"
    android:accessibilityFlags="flagIncludeNotImportantViews|flagReportViewIds"
    android:canRetrieveWindowContent="true"
    android:canRequestTouchExplorationMode="true"
    android:packageNames="com.whatsapp"
    android:accessibilityFeedbackType="feedbackSpoken"

/>

并且 onAccessibilityEvent(...) 是:

@Override
    public void onAccessibilityEvent(AccessibilityEvent event) {


        Log.e("MyService: ","Get Source output: "+event.getSource());
        String get_cord= String.valueOf(event.getSource());

         Log.e("MyService: ","Window id is: "+window_ID);


    }

我得到的输出是(我用粗体突出显示了文本值:

08-18 19:34:29.055 20131-20131/com.example.root.without_root E/MyService:: Get Source output: android.view.accessibility.AccessibilityNodeInfo@801b6813; boundsInParent: Rect(0, 0 - 548, 46); boundsInScreen: Rect(144, 1095 - 692, 1141); packageName: com.device; className: android.widget.TextView; **text: Samsung**; error: null; maxTextLength: -1; contentDescription: null; viewIdResName: com.whatsapp:id/name; checkable: false; checked: false; focusable: false; focused: false; selected: false; clickable: false; longClickable: false; enabled: true; password: false; scrollable: false; actions: [AccessibilityAction: ACTION_SELECT - null, AccessibilityAction: ACTION_CLEAR_SELECTION - null, AccessibilityAction: ACTION_ACCESSIBILITY_FOCUS - null, AccessibilityAction: ACTION_NEXT_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_SET_SELECTION - null]

08-18 19:34:29.058 20131-20131/com.example.root.without_root E/MyService:: Get Source output: android.view.accessibility.AccessibilityNodeInfo@801b6813; boundsInParent: Rect(0, 0 - 548, 46); boundsInScreen: Rect(144, 1095 - 692, 1141); packageName: com.device; className: android.widget.TextView; **text: Samsung**; error: null; maxTextLength: -1; contentDescription: null; viewIdResName: com.whatsapp:id/name; checkable: false; checked: false; focusable: false; focused: false; selected: false; clickable: false; longClickable: false; enabled: true; password: false; scrollable: false; actions: [AccessibilityAction: ACTION_SELECT - null, AccessibilityAction: ACTION_CLEAR_SELECTION - null, AccessibilityAction: ACTION_ACCESSIBILITY_FOCUS - null, AccessibilityAction: ACTION_NEXT_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_SET_SELECTION - null]

08-18 19:34:29.061 20131-20131/com.example.root.without_root E/MyService:: Get Source output: android.view.accessibility.AccessibilityNodeInfo@801b6813; boundsInParent: Rect(0, 0 - 548, 46); boundsInScreen: Rect(144, 1095 - 692, 1141); packageName: com.whatsapp; className: android.widget.TextView; **text: Samsung**; error: null; maxTextLength: -1; contentDescription: null; viewIdResName: com.device:id/name; checkable: false; checked: false; focusable: false; focused: false; selected: false; clickable: false; longClickable: false; enabled: true; password: false; scrollable: false; actions: [AccessibilityAction: ACTION_SELECT - null, AccessibilityAction: ACTION_CLEAR_SELECTION - null, AccessibilityAction: ACTION_ACCESSIBILITY_FOCUS - null, AccessibilityAction: ACTION_NEXT_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_SET_SELECTION - null]
08-18 19:34:29.064 20131-20131/com.example.root.without_root E/MyService:: Get Source output: android.view.accessibility.AccessibilityNodeInfo@801b6bd4; boundsInParent: Rect(0, 0 - 548, 38); boundsInScreen: Rect(144, 1141 - 692, 1179); packageName: com.whatsapp; className: android.widget.TextView; text: ☺; error: null; maxTextLength: -1; contentDescription: null; viewIdResName: com.whatsapp:id/status; checkable: false; checked: false; focusable: false; focused: false; selected: false; clickable: false; longClickable: false; enabled: true; password: false; scrollable: false; actions: [AccessibilityAction: ACTION_SELECT - null, AccessibilityAction: ACTION_CLEAR_SELECTION - null, AccessibilityAction: ACTION_ACCESSIBILITY_FOCUS - null, AccessibilityAction: ACTION_NEXT_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_SET_SELECTION - null]

现在你可以看到它只为 1 个视图运行了 3 次但我希望它只为一个视图运行一次。请帮我找出我怎样才能做到这一点?

我已经尝试将xml中的accessibilityEventTypes的值从“typeAllMasks”更改为“typeWindowContentChanged”和“typeViewFocused”,但没有任何帮助。

我还尝试在 AccessibilityService 的 onAccessibilityEvent(...) 中设置标志,但这也没有奏效,因为该服务每次都在运行(如果有人能让它工作,那就太好了)。

请告诉我如何让它工作!

4

1 回答 1

3

可访问性事件非常频繁地发生。所以,首先,我想让你考虑一下你是否真的想忽略这些事件。您希望解决什么用户体验问题?

特别是,会发生很多的问题是 windowContentChanged 事件和 viewFocused 事件。您可以考虑做的一件事是限制事件。对您的服务配置 xml 的以下更改将限制每个特定事件类型每半秒发生一次。

事件通知超时对于避免过于频繁地向客户端传播事件很有用,因为这是通过昂贵的进程间调用来完成的。可以将超时视为确定事件生成何时稳定的标准。

因此,您的服务配置 xml 将如下所示:

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeWindowContentChanged|typeViewFocused"
android:accessibilityFlags="flagIncludeNotImportantViews|flagReportViewIds"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:packageNames="com.whatsapp"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="500"
/>

但是,值得注意的是,您可能会错过活动。因此,如果您依赖于与这些事件保持同步,您可能希望将其设置得更低。这是一个平衡的行为。

于 2017-08-20T21:21:33.323 回答