Send accessibility event not linked to view

寵の児 提交于 2019-11-30 21:20:40

It looks like the current version of TalkBack ignores announcements if AccessibilityEvent.getSource() returns null, so you're best off using a Toast. This had the added benefit of providing consistent feedback to users whether or not they are using TalkBack.

Toast.makeText(context, /** some text */, Toast.LENGTH_SHORT).show();

Normally, though, you could manually create an AccessibilityEvent and send it through the AccessibilityManager.

AccessibilityManager manager = (AccessibilityManager) context
        .getSystemService(Context.ACCESSIBILITY_SERVICE);
if (manager.isEnabled()) {
    AccessibilityEvent e = AccessibilityEvent.obtain();
    e.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
    e.setClassName(getClass().getName());
    e.setPackageName(context.getPackageName());
    e.getText().add("some text");
    manager.sendAccessibilityEvent(e);
}

You can use the accessibility manager directly (since API 14) like @alanv said. But since API 16, you must provide a view.

final View parentView = view.getParent();
if (parentView != null) {
    final AccessibilityManager a11yManager =
            (AccessibilityManager) view.getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);

    if (a11yManager != null && a11yManager.isEnabled()) {
        final AccessibilityEvent e = AccessibilityEvent.obtain();
        view.onInitializeAccessibilityEvent(e);
        e.getText().add("some text");
        parentView.requestSendAccessibilityEvent(view, e);
    }
}

Try use a Broadcast message, you can send a Intent to Broadcast Receiver, then in the Receiver you can launch a notification or something.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!