我有一个要求,enable或disable文本到语音选项从我的应用程序。
我们将为用户提供一个按钮。如果启用了TTS,用户按下按钮,TTS将被禁用,如果TTS被禁用,用户按下该按钮,TTS将被启用。
对于如何在此按钮上启用/禁用TTS,有什么想法吗?
如果在这方面有任何帮助,我们将不胜感激。
注意:这个应用程序将作为一个系统应用程序进行签名。
发布于 2018-05-24 18:56:39
我找到了这个问题的答案。
答:首先,我们需要了解是否安装了Accessibility Services。
AccessibilityManager am = (AccessibilityManager)(Extension.mainContext.getSystemService(Context.ACCESSIBILITY_SERVICE));
List<AccessibilityServiceInfo> services = am.getInstalledAccessibilityServiceList(); 以上给定的两行将为我们提供Accessibility/Talk Back的安装服务。
在我们拥有了这些服务之后,我们可以为这些服务启用我们想要的权限。请参阅下面的enableTalkBack(),以了解如何做到这一点。
注意:要将写入Secure Settings,您的安卓应用程序需要作为系统应用程序签名,因为只有系统应用程序才有权限将其写入安全设置。
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);
}
}https://stackoverflow.com/questions/50153168
复制相似问题