Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to direct users for enabling accessibility service for my app

I know It's impossible to enable the Accessibility service for apps programmatically, so I'd like to direct users to this screen: System settings --> Accessibility --> app name --> enable/disable screen. Is that possible ?

like image 361
XorOrNor Avatar asked Sep 18 '25 01:09

XorOrNor


2 Answers

You can get them to the Accessibility screen on most devices using ACTION_ACCESSIBILITY_SETTINGS. However:

  • that may not work on all devices, so you will want to just send them to Settings as a fallback, if you get an ActivityNotFoundException

  • there is no way to get them straight to any given app, let alone the enable/disable screen

like image 78
CommonsWare Avatar answered Sep 20 '25 14:09

CommonsWare


You can at least make it reach the app, making the app item blink. It should work for most devices, or at least those that are like of Pixel devices:

    fun <T : AccessibilityService> getRequestAccessibilityPermissionIntents(context: Context, accessibilityService: Class<T>): Array<Intent> {
        var intent = Intent("com.samsung.accessibility.installed_service")
        if (intent.resolveActivity(context.packageManager) == null) {
            intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
        }
        val extraFragmentArgKey = ":settings:fragment_args_key"
        val extraShowFragmentArguments = ":settings:show_fragment_args"
        val bundle = Bundle()
        val showArgs = "${context.packageName}/${accessibilityService.canonicalName!!}"
        bundle.putString(extraFragmentArgKey, showArgs)
        intent.putExtra(extraFragmentArgKey, showArgs)
        intent.putExtra(extraShowFragmentArguments, bundle)
        return arrayOf(intent, Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
            .addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY))
    }

Usage:

    private fun requestAccessibilityPermission() {
        getRequestAccessibilityPermissionIntents(this, MyAccessibilityService::class.java).forEach { intent ->
            try {
                startActivity(intent)
                return
            } catch (e: Exception) {
            }
        }
        //TODO do something here in case it failed
    }
like image 24
android developer Avatar answered Sep 20 '25 14:09

android developer