I was wondering how could I detect if my own service is enabled. So I could check if my service is not enabled, then tell the user to enable it.
Asked
Active
Viewed 2.7k times
30

naXa stands with Ukraine
- 35,493
- 19
- 190
- 259

Rotary Heart
- 1,899
- 3
- 29
- 44
-
Possible duplicate of [Android: How do you check if a particular AccessibilityService is enabled](http://stackoverflow.com/questions/5081145/android-how-do-you-check-if-a-particular-accessibilityservice-is-enabled) – Sam Nov 12 '16 at 21:24
5 Answers
73
Below is the method to check if your accessibility service is enabled or not.
Note: Change value of YOURAccessibilityService
with your Service.
// To check if service is enabled
private boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
final String service = getPackageName() + "/" + YOURAccessibilityService.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(
mContext.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.e(TAG, "Error finding setting, default accessibility to not found: "
+ e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
Log.v(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
if (accessibilityService.equalsIgnoreCase(service)) {
Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}
} else {
Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
}
return false;
}
And to call this method:
if (!isAccessibilitySettingsOn(getApplicationContext())) {
startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
}
This will check and launch accessibility settings if not enabled.

Antoine
- 3,880
- 2
- 26
- 44

Pankaj Kumar
- 81,967
- 29
- 167
- 186
-
1@Pankaj what if the getint method is throwing the settings not found exception. what can we do in that case. I tried it on KITKAT emulator and it throws this Settings not found exception but setting is there. Is there some other method that we can use in this case. – dirtydexter Sep 25 '14 at 13:35
-
1Thanks, working as expected. It's better to use getPackageName() at line final String service = mContext.getPackageName() + "/" + mContext.getPackageName() + ".YourAccessibilityService"; – user207064 Jun 16 '15 at 05:53
-
2@user207064 This method has been written for any class. It can be moved into any Component or non-component class into android. As you knwo non-component does not have context object, so this method was a parameterised method and uses `mContext.getPackageName()`. If you are using this method into Component class then yes you no need to use mContext. – Pankaj Kumar Jun 16 '15 at 06:07
9
This is a modified version of Jakub Bláha's answer in java.
public boolean isAccessServiceEnabled(Context context, Class accessibilityServiceClass)
{
String prefString = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
return prefString!= null && prefString.contains(context.getPackageName() + "/" + accessibilityServiceClass.getName());
}

danial abadi ghadim
- 395
- 1
- 3
- 10
1
This is somehow a smaller version, but it is working.
fun isAccessServiceEnabled(context: Context): Boolean {
val prefString =
Settings.Secure.getString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
return prefString.contains("${context.packageName}/${context.packageName}.${context.getString(R.string.access_service_name)}")
}
Feel free to correct me if there is something missing.

Jakub Bláha
- 1,491
- 5
- 22
- 43
-
-
2@SomeoneSomewhere Take a look at the following link. Every service has to have a name. I have stored the service name in the string resources in my case. https://developer.android.com/guide/topics/ui/accessibility/service#service-declaration – Jakub Bláha Jun 27 '20 at 17:09
1
1:Kotlin Based answer 2:Added a current package name check as well cos it will return true only if accessibility service will be Enabled for current package, Recently it was returning true if any package accessibility service enabled
private fun checkAccessibilityPermission(): Boolean {
var isAccessibilityEnabled = false
(requireActivity().getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager).apply {
installedAccessibilityServiceList.forEach { installedService ->
installedService.resolveInfo.serviceInfo.apply {
if (getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK).any { it.resolveInfo.serviceInfo.packageName == packageName && it.resolveInfo.serviceInfo.name == name && permission == Manifest.permission.BIND_ACCESSIBILITY_SERVICE && it.resolveInfo.serviceInfo.packageName == requireActivity().packageName })
isAccessibilityEnabled = true
}
}
}
if (isAccessibilityEnabled.not())
startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
})
return isAccessibilityEnabled
}

Yasir Tanveer
- 91
- 5
-
1Can you explain your code ? Please [edit] your answer to show what you change, why etc... – Elikill58 Nov 01 '21 at 14:31
-
@Elikill58 I have added a app package name check as well, it will return true only in case accessibility service enabled for currently package name! **Settings.Secure.getString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)** -> this answer is not working its return 1 if accessibility service is off – Yasir Tanveer Nov 26 '21 at 08:14
0
Kotlin version based on answer of @Pankaj Kumar
inline fun <reified T> Context.isAccessibilityEnabled(): Boolean {
var enabled = 0
try {
enabled = Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED)
} catch (e: SettingNotFoundException) {
Timber.e(e)
}
if (enabled == 1) {
val name = ComponentName(applicationContext, T::class.java)
val services = Settings.Secure.getString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
return services?.contains(name.flattenToString()) ?: false
}
return false
}

Vlad
- 7,997
- 3
- 56
- 43