首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android中启用文本到另一个应用程序的语音/回覆

如何在android中启用文本到另一个应用程序的语音/回覆
EN

Stack Overflow用户
提问于 2018-05-03 10:47:09
回答 1查看 765关注 0票数 0

我有一个要求,enabledisable文本到语音选项从我的应用程序。

我们将为用户提供一个按钮。如果启用了TTS,用户按下按钮,TTS将被禁用,如果TTS被禁用,用户按下该按钮,TTS将被启用。

对于如何在此按钮上启用/禁用TTS,有什么想法吗?

如果在这方面有任何帮助,我们将不胜感激。

注意:这个应用程序将作为一个系统应用程序进行签名。

EN

回答 1

Stack Overflow用户

发布于 2018-05-24 18:56:39

我找到了这个问题的答案。

答:首先,我们需要了解是否安装了Accessibility Services

代码语言:javascript
复制
AccessibilityManager am = (AccessibilityManager)(Extension.mainContext.getSystemService(Context.ACCESSIBILITY_SERVICE));
List<AccessibilityServiceInfo> services = am.getInstalledAccessibilityServiceList();  

以上给定的两行将为我们提供Accessibility/Talk Back的安装服务。

在我们拥有了这些服务之后,我们可以为这些服务启用我们想要的权限。请参阅下面的enableTalkBack(),以了解如何做到这一点。

注意:要将写入Secure Settings,您的安卓应用程序需要作为系统应用程序签名,因为只有系统应用程序才有权限将其写入安全设置。

代码语言:javascript
复制
public static void enableTalkBack()
{
    try {
         AccessibilityManager am = (AccessibilityManager)(Extension.mainContext.getSystemService(Context.ACCESSIBILITY_SERVICE));
         List<AccessibilityServiceInfo> services = am.getInstalledAccessibilityServiceList();

        if (services.isEmpty()) {
            return;
        }

        AccessibilityServiceInfo service = services.get(0);

        boolean enableTouchExploration = (service.flags
                & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0;
        // Try to find a service supporting explore by touch.
        if (!enableTouchExploration) {
            final int serviceCount = services.size();
            for (int i = 1; i < serviceCount; i++) {
                AccessibilityServiceInfo candidate = services.get(i);
                if ((candidate.flags & AccessibilityServiceInfo
                            .FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0) {
                    enableTouchExploration = true;
                    service = candidate;
                    break;
                }
            }
        }

        ServiceInfo serviceInfo = service.getResolveInfo().serviceInfo;
        ComponentName componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name);
        String enabledServiceString = componentName.flattenToString();
        ContentResolver resolver = Extension.mainContext.getContentResolver();

        Settings.Secure.putString(resolver, "enabled_accessibility_services", enabledServiceString);
        Settings.Secure.putString(resolver,
                "touch_exploration_granted_accessibility_services",
                enabledServiceString);
        if (enableTouchExploration) {
            Settings.Secure.putInt(resolver, "touch_exploration_enabled", 1);
        }
        Settings.Secure.putInt(resolver, "accessibility_script_injection", 1);
        Settings.Secure.putInt(resolver, "accessibility_enabled", 1);
    }
    catch(Exception e) {
        Log.e("Device", "Failed to enable accessibility: " + e);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50153168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档