7

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 accessibility event (e.g. talkback saying "Data downloaded") when a AsyncTask has finished?

rds
  • 26,253
  • 19
  • 107
  • 134
Alex Curran
  • 8,818
  • 6
  • 49
  • 55
  • Perhaps reconsider if that's the best place to do it - you'll be providing feedback only to users of TalkBack but not visually? This makes it inaccessible to non-TalkBack users. – ataulm Jun 10 '16 at 13:52

3 Answers3

12

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);
}
alanv
  • 23,966
  • 4
  • 93
  • 80
  • Not had any luck when trying this, and Talkback doesn't say the text. Any ideas? – Alex Curran Feb 27 '14 at 10:34
  • Sorry, the setAction() call should have been setEventType(). – alanv Mar 03 '14 at 06:15
  • Also, it looks like the most recent version of TalkBack will reject events without a source. This seems like a bug. In the meantime, you might just want to show a Toast. – alanv Mar 03 '14 at 06:31
3

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);
    }
}
rds
  • 26,253
  • 19
  • 107
  • 134
-2

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

kodamirmo
  • 92
  • 2
  • 8