How to Programmatically Enable/Disable Accessibility Service in Android

61,571

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"

Share:
61,571
Abilash
Author by

Abilash

Updated on November 21, 2021

Comments

  • Abilash
    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
    Yeung almost 10 years
    From 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 call Settings.Secure.putString(...) once the app has system privilege?
  • Rupert Rawnsley
    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
    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
    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
    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
    Alex almost 9 years
    Maybe a stupid question. Whats the packagename and whats the classname? com.somepackage/MyAcessibilityService is this the right way?
  • Kevin
    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
    android developer over 7 years
    Does it work even on android 7? Also, how do I enable usage access via adb?
  • android developer
    android developer about 7 years
    Is it possible to go directly to the specific app's screen that has the toggle there ?
  • android developer
    android developer about 7 years
    How 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
    Vadiraj Purohit over 6 years
    OP is about how to programmatically enable/disable the accessibility service not using adb shell
  • Dhruv Kaushal
    Dhruv Kaushal about 6 years
    @androiddeveloper android.permission.WRITE_SECURE_SETTINGS is given to System apps only!
  • android developer
    android developer about 6 years
    Well 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
    Maxim Blumental almost 6 years
    If you can run it in console, then you can run it programmatically in test: InstrumentationRegistry.getInstrumentation().getUiAutomation‌​(UiAutomation.FLAG_D‌​ONT_SUPPRESS_ACCESSI‌​BILITY_SERVICES).exe‌​cuteShellCommand("..‌​.")
  • 4face
    4face almost 6 years
    But how to get the instance of the service? I don't think I could simply call the primary constructor :/
  • MyPreciousss
    MyPreciousss over 4 years
    I 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
    binarynoise about 4 years
    use a static reference like this: static WeakReference<MyAccessibilityService> ref = null and then in your service's onCreate() use if(ref != null && ref.get() != null) ref.get().disableSelf();. Use the WeakReference to avoid memory leaks
  • letroll
    letroll almost 4 years
    @Kushal you can grant this permission from adb ( adb shell pm grant yourpackagename android.permission.WRITE_SECURE_SETTINGS)