How to Programmatically Enable/Disable Accessibility Service in Android
Solution 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
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
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.
Solution 2
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.
Solution 3
In Android 7 (API 24), an AccessibilityService can programmatically disable itself by calling the disableSelf() method.
Solution 4
AccessibilityService is special and cannot be started programmatically.
Solution 5
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"
Abilash
Updated on November 21, 2021Comments
-
Abilash over 2 years
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.
-
Yeung almost 10 yearsFrom this, the enabled AccessibilityService is stored as a String from
Settings.Secure.getString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
. Isn't it possible to enable AccessibilityService by following the String format and callSettings.Secure.putString(...)
once the app has system privilege? -
Rupert Rawnsley almost 10 years@Yeung In principle, but in practice I've found this to be hit-and-miss depending on the version of Android and the device. The process that controls Accessibility Services doesn't always see those changes without a reboot. In our app we direct the user to the settings page and have them enable the service for us.
-
Kushal over 9 years<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" /> is given to only system applications so this solution will not work for non-system applications
-
Kushal over 9 years@Yeung Settings.Secure.putString(...) works only for system applications. For other applications, Android do not allow to turn accessibility service programmatically
-
Kevin over 9 years@Kushal Yes, this solution works only for system applications (or non-sys apps if your phone is rooted). And I think a non-system app might not be able to programmically enable an Accessibility Service due to security issues.
-
Alex almost 9 yearsMaybe a stupid question. Whats the packagename and whats the classname? com.somepackage/MyAcessibilityService is this the right way?
-
Kevin almost 9 years@Alex Sorry it's been quite a long time and I don't have the code now. But as far as I remember, it should be com.somepackage/.MyAcessibilityService or com.somepackage/com.sompackage.MyAcessibilityService. Also, I found that a newer version of Android might simply ignore this code. I had to write the database directly. Might be a little bit ugly, but it worked. FYI
-
android developer over 7 yearsDoes it work even on android 7? Also, how do I enable usage access via adb?
-
android developer about 7 yearsIs it possible to go directly to the specific app's screen that has the toggle there ?
-
android developer about 7 yearsHow did you guys succeed using this? It shows me an exception: java.lang.SecurityException: Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS
-
Vadiraj Purohit over 6 yearsOP is about how to programmatically enable/disable the accessibility service not using adb shell
-
Dhruv Kaushal about 6 years@androiddeveloper android.permission.WRITE_SECURE_SETTINGS is given to System apps only!
-
android developer about 6 yearsWell I don't see the folder "/data/data/com.android.providers.settings/databases/" at all either, even though I have a rooted device.
-
Maxim Blumental almost 6 yearsIf you can run it in console, then you can run it programmatically in test:
InstrumentationRegistry.getInstrumentation().getUiAutomation(UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES).executeShellCommand("...")
-
4face almost 6 yearsBut how to get the instance of the service? I don't think I could simply call the primary constructor :/
-
MyPreciousss over 4 yearsI came across this while trying to figure out how to enable via adb for DarQ... Here is the command that worked:
adb shell settings put secure enabled_accessibility_services com.kieronquinn.app.darq/.services.DarqBackgroundService
-
binarynoise about 4 yearsuse a static reference like this:
static WeakReference<MyAccessibilityService> ref = null
and then in your service'sonCreate()
useif(ref != null && ref.get() != null) ref.get().disableSelf();
. Use the WeakReference to avoid memory leaks -
letroll almost 4 years@Kushal you can grant this permission from adb ( adb shell pm grant yourpackagename android.permission.WRITE_SECURE_SETTINGS)