简体   繁体   English

AccessibilityService不返回视图ID

[英]AccessibilityService not returning view ids

I'm writing an acessibility service in Android which relies on getting the view id of the currently selected view, however on some devices (Nexus 6P 6.0.1, Samsung Galaxy S6 edge+ 5 + 6.0.1) I get no view id through and on others (HTC One M8 5.0.1) it comes through fine. 我正在Android中编写一个可访问性服务,它依赖于获取当前所选视图的视图ID,但是在某些设备上(Nexus 6P 6.0.1,三星Galaxy S6 edge + 5 + 6.0.1)我没有查看id和在其他人(HTC One M8 5.0.1)它通过罚款。 Because it works fine on some devices, I'm sure there's not a problem with my code, however I've posted a minimal test case below. 因为它在某些设备上运行良好,我确信我的代码没有问题,但是我在下面发布了一个最小的测试用例。

Can anyone can help me get my service reporting ids across all devices? 任何人都可以帮助我在所有设备上获取服务报告ID吗?

The A11y service A11y服务

public class ViewIdLoggingAccessibilityService extends AccessibilityService {
  @Override
  public void onAccessibilityEvent(AccessibilityEvent event) {
      AccessibilityNodeInfo source = event.getSource();
      if (source == null) {
          Log.d("onAccessibilityEvent", "source was null for: " + event);
      } else {
          String viewIdResourceName = source.getViewIdResourceName();
          Log.d("onAccessibilityEvent", "viewid: " + viewIdResourceName);
      }
  }

  @Override
  public void onInterrupt() {
      Log.d("!# onInterrupt", "called");
  }
}

The AndroidManifest.xml AndroidManifest.xml

<manifest package="com.example.a11yservice"
          xmlns:android="http://schemas.android.com/apk/res/android"
    >

  <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme"
      >

    <service
        android:name=".ViewIdLoggingAccessibilityService"
        android:label="@string/view_id_logging_service_label"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        >
      <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
      </intent-filter>
      <meta-data
          android:name="android.accessibilityservice"
          android:resource="@xml/serviceconfig"
          />
    </service>

  </application>

</manifest>

serviceconfig.xml serviceconfig.xml

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagReportViewIds"
    android:canRetrieveWindowContent="true"
    android:notificationTimeout="100" />

It's known issue and was posted to AOSP issue tracker some time ago. 它已知问题,并且不久前被发布到AOSP问题跟踪器。 You can check status of issue here: " Accessibility Issue: flagReportViewIds has no effect on real devices in Android M ". 您可以在此处检查问题状态:“ 辅助功能问题:flagReportViewIds对Android M中的实际设备没有影响 ”。

Unfortunately issue exists even on latest Android 7.0 but I found simple workaround. 不幸的是,即使在最新的Android 7.0上也存在问题但我找到了简单的解决方 You can call refresh() method on AccessibilityNodeInfo object to refresh it and collect all missed data including id. 您可以在AccessibilityNodeInfo对象上调用refresh()方法来刷新它并收集包括id在内的所有遗漏数据。

Something like that: 像这样的东西:

public void onAccessibilityEvent(AccessibilityEvent event) {
    AccessibilityNodeInfo ani = event.getSource();
    if (ani != null) {
        ani.refresh(); // to fix issue with viewIdResName = null on Android 6+
    }
}

I found that if the viewId isn't available, querying the focused element seems to work: 我发现如果viewId不可用,查询焦点元素似乎有效:

    AccessibilityNodeInfo source = findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
    viewIdResourceName = source.getViewIdResourceName();

returns the correct view id 返回正确的视图ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM