android - 无法在 Android 中发送辅助功能事件

标签 android android-snackbar accessibility

我正在尝试在显示 snackbar 后立即发送可访问性。但是没有播放。这是我在 onStart() 生命周期方法中的代码:

            final Snackbar snackbar = Snackbar
                       .make(findViewById(R.id.container), "snackbars are cool", Snackbar.LENGTH_INDEFINITE);
               snackbar.setActionTextColor(getResources().getColor(R.color.snackbar_actions));


        View snackbarView = snackbar.getView();
   snackbarView.setBackgroundColor(Color.DKGRAY);
   TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
   textView.setTextColor(Color.WHITE);
   snackbar.show();


//time to send a accessibility event below
       final ViewGroup parentView = (ViewGroup)findViewById(R.id.container);
       if (parentView != null) {
           final AccessibilityManager a11yManager =
                   (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);

           if (a11yManager != null && a11yManager.isEnabled()) {
               final AccessibilityEvent e = AccessibilityEvent.obtain();
               snackbarView.onInitializeAccessibilityEvent(e);
               e.getText().add("some more text to say");
//nothing happens here

           parentView.requestSendAccessibilityEvent(snackbarView, e);
       }

    }

最佳答案

一些开发人员为此编写了修复程序。我的另一个问题是出于某种原因我不应该在 onStart 中调用它。无论如何,我创建了一个 utils 方法并使用了如下所示的开发人员命令:

if (!mA11yManager.isEnabled()) {
    return;
}

// Prior to SDK 16, announcements could only be made through FOCUSED
// events. Jelly Bean (SDK 16) added support for speaking text verbatim
// using the ANNOUNCEMENT event type.
final int eventType;
if (Build.VERSION.SDK_INT < 16) {
    eventType = AccessibilityEvent.TYPE_VIEW_FOCUSED;
} else {
    eventType = AccessibilityEventCompat.TYPE_ANNOUNCEMENT;
}

// Construct an accessibility event with the minimum recommended
// attributes. An event without a class name or package may be dropped.
final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
event.getText().add(text);
event.setEnabled(isEnabled());
event.setClassName(getClass().getName());
event.setPackageName(mContext.getPackageName());

// JellyBean MR1 requires a source view to set the window ID.
final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
record.setSource(this);

// Sends the event directly through the accessibility manager. If your
// application only targets SDK 14+, you should just call
// getParent().requestSendAccessibilityEvent(this, event);
mA11yManager.sendAccessibilityEvent(event);}

可以看到here

关于android - 无法在 Android 中发送辅助功能事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33397730/

相关文章:

java - 从另一个 Activity 将项目添加到回收者 View

android - jQuery 移动文本字段不会失去焦点

android - 如何禁用 snackbar 的滑动关闭行为

android - 如何在没有支持库的情况下使用 Snackbar?

xhtml - <em> 中的 <strong> 还是 <strong> 中的 <em> 重要吗?

html - 使用特定的 target_country 和国家元标记

android - 在Android上强制溢出菜单按钮

java - AndroidX 迁移 AndroidTestCase、AndroidJUnit4 和 InstrumentationRegistry.getTargetContext 后已弃用

android - 关闭 Snackbar/ViewDragHelper 时出现 ArrayIndexOutOfBoundsException

html - 可通过键盘导航的纯文本 HTML 单选按钮?