如何宣布API 16之前的辅助功能?

时间:2015-09-03 16:26:20

标签: android accessibility

API 16中添加了方法announceForAccessibility(CharSequence text),可让您在需要时触发通知。

我尝试使用辅助功能管理器在api级别执行相同操作< 16但似乎API 16中还添加了TYPE_ANNOUNCEMENT

是否有解决方法或支持方法允许我宣布运行api的设备的辅助功能< 16?

2 个答案:

答案 0 :(得分:2)

请避免announceForAccessibility()或在应用程序中发送TYPE_ANNOUNCEMENT,除非您传达严重消息以使用该设备。这些活动的发言将不会中断,可能会干扰导航或用户与TalkBack的互动。

答案 1 :(得分:1)

如果答案有所下降,请将此作为答案添加为alanv。最初发布here

  

您需要使用轻微的解决方法并触发公告   通过在ICS上发送VIEW_FOCUSED事件,或使用   JellyBean及更高版本的announceForAccessibility API。这将   需要support-v4库,看起来像这样:

/** 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);
}