如何宣布 pre API 16 的可访问性?
How to Announce for Accessibility for pre API 16?
announceForAccessibility(CharSequence text) 方法已添加到 API 16 中,可让您在需要时触发通知。
我尝试使用可访问性管理器在 api 级别 < 16 中执行相同操作,但似乎 TYPE_ANNOUNCEMENT
也在 API 16
中添加
是否有解决方法或支持方法允许我宣布设备 运行 api < 16 的辅助功能?
将此添加为 alanv 的答案,以防答案消失。最初发布 here:
you will need to use a slight workaround and trigger an announcement
by sending a VIEW_FOCUSED event on ICS, or use the
announceForAccessibility API on JellyBean and above. Which would
require the support-v4 library and would look like this:
/** The parent context. Used to obtain string resources. */
private final Context mContext;
/**
* The accessibility manager for this context. This is used to check the
* accessibility enabled state, as well as to send raw accessibility events.
*/
private final AccessibilityManager mA11yManager;
/**
* Generates and dispatches an SDK-specific spoken announcement.
* <p>
* For backwards compatibility, we're constructing an event from scratch
* using the appropriate event type. If your application only targets SDK
* 16+, you can just call View.announceForAccessibility(CharSequence).
* </p>
*
* @param text The text to announce.
*/
private void announceForAccessibilityCompat(CharSequence text) {
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);
}
请避免在应用程序中announceForAccessibility()
或发送TYPE_ANNOUNCEMENT
,除非您传达的消息对设备的使用至关重要。来自这些事件的语音将不会被打断,并且可能会干扰导航或用户与 TalkBack 的交互。
announceForAccessibility(CharSequence text) 方法已添加到 API 16 中,可让您在需要时触发通知。
我尝试使用可访问性管理器在 api 级别 < 16 中执行相同操作,但似乎 TYPE_ANNOUNCEMENT
也在 API 16
是否有解决方法或支持方法允许我宣布设备 运行 api < 16 的辅助功能?
将此添加为 alanv 的答案,以防答案消失。最初发布 here:
you will need to use a slight workaround and trigger an announcement by sending a VIEW_FOCUSED event on ICS, or use the announceForAccessibility API on JellyBean and above. Which would require the support-v4 library and would look like this:
/** The parent context. Used to obtain string resources. */
private final Context mContext;
/**
* The accessibility manager for this context. This is used to check the
* accessibility enabled state, as well as to send raw accessibility events.
*/
private final AccessibilityManager mA11yManager;
/**
* Generates and dispatches an SDK-specific spoken announcement.
* <p>
* For backwards compatibility, we're constructing an event from scratch
* using the appropriate event type. If your application only targets SDK
* 16+, you can just call View.announceForAccessibility(CharSequence).
* </p>
*
* @param text The text to announce.
*/
private void announceForAccessibilityCompat(CharSequence text) {
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);
}
请避免在应用程序中announceForAccessibility()
或发送TYPE_ANNOUNCEMENT
,除非您传达的消息对设备的使用至关重要。来自这些事件的语音将不会被打断,并且可能会干扰导航或用户与 TalkBack 的交互。