且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Android 中以编程方式启用/禁用辅助功能服务

更新时间:2023-01-02 14:58:13

我通过致电找到了一个适合我的解决方案

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.