How to Programmatically Enable/Disable Accessibility Service in Android

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

I would like to programmatically enable/disable Accessibility Services listed under Settings->Accessibility option.

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.

Please, provide me your views.

回答1:

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

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

 

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.

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.



回答2:

AccessibilityService is special and cannot be started programmatically.



回答3:

In Android 7 (API 24), an AccessibilityService can programmatically disable itself by calling the disableSelf() method.



回答4:

The best you can do is manualy open the accessibilty settings with:

Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)

and start the intent - you can also do it from the prefernece xml file:

intent android:action="android.settings.ACCESSIBILITY_SETTINGS"



回答5:

From Android 6.0 you can use:

adb shell settings put secure enabled_accessibility_services packagname/servicename

The settings.db from old releases is no longer present on Android 6.0.



回答6:

Just in case anybody still seeks to turn off talkback from adb when you are stuck in your lock screen entering the password or pin. One thing you can try is adb shell am force-stop com.google.android.marvin.talkback



回答7:

I found this post: How to programmatically check if a service is declared in AndroidManifest.xml?. The top answer talks about PackageManager, which tells you what is running.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!