[Android]: TalkBack doesn't speak when AccessibilityEvent is triggered

▼魔方 西西 提交于 2020-01-25 06:12:48

问题


I'm creating a custom view (extending View class) and playing with Accessibility API to understand, how it works. Below is my code, where:

  1. I check for MotionEvent equal to ACTION_HOVER_ENTER and then inside it send AccessibilityEvent of type TYPE_VIEW_HOVER_ENTER.
  2. I'm catching my AccessibilityEvent in onPopulateAccessibilityEvent. Then I'm adding my custom text into event's text, which is added just fine.

As a result, when I hover over my view, all is working fine (confirmed by my logs), but TalkBack doesn't say my custom text. The only way I was able to make TalkBack to say custom text is to use setContentDescription("custom text...").

But, the way I understand the API, it should be possible to set different text depending on the type of MotionEvent and correspondingly a type of AccessibilityType.

My question - can someone explain to me please, how can I make TalkBack to speak my custom text, which I can set depending on type of event?

@Override
    public boolean onHoverEvent(MotionEvent event) {
        final int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_HOVER_ENTER:
                Log.d("test", "onHoverEvent: ACTION_HOVER_ENTER"); // <-- this is triggered correctly on hover enter
                sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
                return true;
        }
        return super.onHoverEvent(event);
    }

    @Override
    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
        super.onPopulateAccessibilityEvent(event);
        if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_HOVER_ENTER) {
            Log.d("test", "onPopulateAccessibilityEvent");
            CharSequence text = "this is a test";
            Log.d("test", "text before: " + event.getText()); // text before is empty, i.e. "[]"
            event.getText().add(text);
            Log.d("test", "text after: " + event.getText()); // text after is "[this is a test]", but TalkBack is silent
        }
    }


回答1:


refer to this answer

You may try to enable/disable accessibility setting in following way.

Settings.Secure.putString(getContentResolver(), 
    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "pkgname/classname");
Settings.Secure.putString(getContentResolver(), 
    Settings.Secure.ACCESSIBILITY_ENABLED, "1");

Where the pkgname is your package name and the classname is the class name of your accessibility service.

If you need to enable several services or you don't want to destory the previous settings you might want to use : to seperate other services.

Also you might need to run as a system application and you might need the following permissions

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

this might not work on some versions of Android.

Also refer other answers to this question

PS. If it doesn't work, maybe you could find some luck in /data/data/com.android.providers.settings/databases/settings.db/secure, that's where Android stores secure settings.



来源:https://stackoverflow.com/questions/54307887/android-talkback-doesnt-speak-when-accessibilityevent-is-triggered

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