我正在尝试在显示快餐栏后立即发送辅助功能。但是什么也没玩。这是我的代码,它位于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