Send accessibility event not linked to view

后端 未结 3 1643
臣服心动
臣服心动 2021-01-05 22:45

We\'re looking to send an accessibility event (which would be picked up by TalkBack etc.) which isn\'t linked to a view.

For example, how could I send an accessibili

3条回答
  •  醉话见心
    2021-01-05 23:21

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

提交回复
热议问题