Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Accessibility: Reading custom text on activity launch

I want to read some instruction via accessibility as soon as My activity is launched. How can I do this? (I don't want to read out the activity label)

like image 741
Amit Avatar asked Jan 04 '23 13:01

Amit


1 Answers

You can do this by posting an accessibility announcement event.

AccessibilityManager manager = (AccessibilityManager)mService.getSystemService(Context.ACCESSIBILITY_SERVICE);

if (manager.isEnabled()) {

    AccessibilityEvent e = AccessibilityEvent.obtain();
    e.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
    e.getText().add(message);

    //There may be other things you need to add like class/packagename, I'm doing this from memory on my non-dev machine, so if this isn't quite right I apologize, I promise it's super close! :)

    manager.sendAccessibilityEvent(e);
}

Note that you might not be able to do this in your activities onCreate method. There are subtle race conditions with the attachment of accessibility services. If indeed placing this in onCreate does not work, try adding it to a Runnable with a delay, or in onPostResume, which I believe is the latest callback in that chain. Ultimately though, if this is an issue, the delayed Runnable is the only reliable workaround.

like image 174
ChrisCM Avatar answered Jan 13 '23 09:01

ChrisCM