How to Programmatically Enable/Disable Accessibility Service in Android

Programmatically enabling/disabling accessibility settings on Android device

Only system apps can enable/disable accessibility service programatically.
System apps can directly write in settings secure db to start accessibility service.

Settings.Secure.putString(getContentResolver(),Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "com.packagename/com.packagename.componentname");

Following permission is required to write in settings secure db:

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

For non system apps, only way to start accessibility service is direct them to accessibility settings screen via intent and let user manually start the service :

Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);

How can I programmatically start and stop an AccessibilityService?

Taken from the Lifecycle section of the AccessibilityService documentation:

The lifecycle of an accessibility service is managed exclusively by the system and follows the established service life cycle. Starting an accessibility service is triggered exclusively by the user explicitly turning the service on in device settings. After the system binds to a service, it calls onServiceConnected(). This method can be overriden by clients that want to perform post binding setup.

An accessibility service stops either when the user turns it off in device settings or when it calls disableSelf() (In Android 7.0 (API 24) and later).

How Enable/Disable Accessibility Service When Lock/Unlock Screen in Android

I found the answer : https://stackoverflow.com/a/45100001/15772513
I put it on the Accessibility Service class and call disableSelf() when the screen is unlock, and call that function in OnCreate() of AccessibilityService class.

I hope this will help you.

How can I start the Accessibility Settings Page of my APP in Android?

Maybe the code below can help you :

Intent intent = new Intent();
intent.setClassName("com.android.settings",
"com.android.settings.Settings");
intent.setAction(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
"the fragment which you want show");
intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS,
extras);
startActivity(intent);

Or you can search the Key Word "Fragment Injection" for more information;
Check out this link,it is useful for you case:

  • http://securityintelligence.com/new-vulnerability-android-framework-fragment-injection

How to direct users for enabling accessibility service for my app

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



Related Topics



Leave a reply



Submit