How to Programmatically Enable/Disable Accessibility Service in Android

后端 未结 9 556
暗喜
暗喜 2020-11-27 15:38

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

I could start Accessibility Intent like below:

9条回答
  •  心在旅途
    2020-11-27 15:52

    In instrumented tests I start my accessibility service like this:

    private fun enableAccessibilityService() {
        val packageName = "com.example"
        val className = "$packageName.service.MyService"
        val string = "enabled_accessibility_services"
        val cmd = "settings put secure $string $packageName/$className"
        InstrumentationRegistry.getInstrumentation()
          .getUiAutomation(UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES)
          .executeShellCommand(cmd)
          .close()
        TimeUnit.SECONDS.sleep(3)
    }
    

    I tested on Android 6 and 8. This also works for non-system apps.

提交回复
热议问题