如何在 Android 中以编程方式启用/禁用辅助功能服务 [英] How to Programmatically Enable/Disable Accessibility Service in Android
问题描述
我想以编程方式启用/禁用设置->辅助功能选项下列出的辅助功能服务.
I would like to programmatically enable/disable Accessibility Services listed under Settings->Accessibility option.
我可以像下面这样启动 Accessibility Intent:
I could start Accessibility Intent like below:
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(intent, 0);
但我不知道如何通过我的代码启用视图中列出的服务.
But I don't have any idea on how to enable the services listed in the view through my code.
请告诉我你的观点.
推荐答案
我通过致电找到了一个适合我的解决方案
I found a solution worked for me by calling
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" />
但是,根据 8@comment37071078Rupert Rawnsley 这可能不适用于某些版本的 Android,我正在使用 Android 4.0.4 并希望它适用于您.
However, according to @Rupert Rawnsley this might not work on some versions of Android, I am working on Android 4.0.4 and hope it works for you.
附注.如果它不起作用,也许您可以在 /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 中以编程方式启用/禁用辅助功能服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!