[Android]:触发AccessibilityEvent时,TalkBack不讲话 [英] [Android]: TalkBack doesn't speak when AccessibilityEvent is triggered

查看:832
本文介绍了[Android]:触发AccessibilityEvent时,TalkBack不讲话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义视图(扩展View类),并使用Accessibility API来了解其工作原理。
下面是我的代码,其中:

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. 我检查了 MotionEvent 等于 ACTION_HOVER_ENTER ,然后在其中发送 TYPE_VIEW_HOVER_ENTER 类型的AccessibilityEvent。

  2. 我在 onPopulateAccessibilityEvent 中捕获了我的 AccessibilityEvent 。然后,我将自定义文本添加到活动文本中,只需添加即可。

  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.

结果是,当我将鼠标悬停在我的视图上时,一切都很好(由我的日志确认),但是TalkBack却没有说我的自定义文字。我可以使话语提示说出自定义文本的唯一方法是使用 setContentDescription(自定义文本...)

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...").

但是,据我理解API的方式,应该可以根据 MotionEvent 以及相应的 AccessibilityType 类型。

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


推荐答案

请参考此答案

您可以尝试通过以下方式启用/禁用辅助功能设置。

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

其中pkgname是您的包名称,classname是您的可访问性服务的类名称。

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" />

这可能不适用于某些版本的Android。

this might not work on some versions of Android.

另请参考其他答案此问题

PS。如果它不起作用,也许您可​​以在/data/data/com.android.providers.settings/databases/settings.db/secure中找到运气,这就是Android存储安全设置的地方。

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.

这篇关于[Android]:触发AccessibilityEvent时,TalkBack不讲话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆