Android: How to Check If a Particular Accessibilityservice Is Enabled

Android: How do you check if a particular AccessibilityService is enabled

I worked this one out myself in the end:

public boolean isAccessibilityEnabled() {
int accessibilityEnabled = 0;
final String LIGHTFLOW_ACCESSIBILITY_SERVICE = "com.example.test/com.example.text.ccessibilityService";
boolean accessibilityFound = false;
try {
accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(),android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.d(LOGTAG, "ACCESSIBILITY: " + accessibilityEnabled);
} catch (SettingNotFoundException e) {
Log.d(LOGTAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
}

TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

if (accessibilityEnabled==1) {
Log.d(LOGTAG, "***ACCESSIBILIY IS ENABLED***: ");

String settingValue = Settings.Secure.getString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
Log.d(LOGTAG, "Setting: " + settingValue);
if (settingValue != null) {
TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
splitter.setString(settingValue);
while (splitter.hasNext()) {
String accessabilityService = splitter.next();
Log.d(LOGTAG, "Setting: " + accessabilityService);
if (accessabilityService.equalsIgnoreCase(ACCESSIBILITY_SERVICE_NAME)){
Log.d(LOGTAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}

Log.d(LOGTAG, "***END***");
}
else {
Log.d(LOGTAG, "***ACCESSIBILIY IS DISABLED***");
}
return accessibilityFound;
}

Detect if my accessibility service is enabled

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.

how to detect accessibility settings on android is enabled/disabled

    AccessibilityManager am = (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);

Class clazz = am.getClass();
Method m = null;
try {
m = clazz.getMethod("isHighTextContrastEnabled",null);
} catch (NoSuchMethodException e) {
Log.w("FAIL", "isHighTextContrastEnabled not found in AccessibilityManager");
}

Object result = null;
try {
result = m.invoke(am, null);
if (result != null && result instanceof Boolean) {
Boolean b = (Boolean)result;
Log.d("result", "b =" + b);
}
} catch (Exception e) {
android.util.Log.d("fail", "isHighTextContrastEnabled invoked with an exception" + e.getMessage());
return;
}

and I do test, it return false, so it works

Check Permission for BIND_ACCESSIBILITY_SERVICE

As far as I understand you don't need to check for this permission. It is declared in service tag to ensure that only Android system can bind to it (and prohibit any other 3rd party apps to bind to it).

Can I ask why do you want to check this permission as normal permission?

For more information about AccessibilityService please take a look into documentation: https://developer.android.com/guide/topics/ui/accessibility/service#manifest

Update after clarification:

Checking if your Accessibility Service is turned on by the user may be tricky. As far as I know it can't be done via Accessibility API. Some workarounds can be found here:
Detect if my accessibility service is enabled
and here:
Android: How do you check if a particular AccessibilityService is enabled



Related Topics



Leave a reply



Submit