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
How to Set Shadow to a View in Android
How to Get the Position Selected in a Recyclerview
Android Slidingdrawer from Top
Android:Fill Spinner from Java Code Programmatically
Android: Webview Inside Dialog or Popup
Getting MAC Address in Android 6.0
Theme/Style Is Not Applied When Inflater Used with Applicationcontext
How to Call Methods of a Service from Activity
Why Does Alertdialog.Builder(Context Context) Only Accepts Activity as a Parameter
How to Handle Outofmemoryerror
How to Crop the Parsed Image in Android
Could Not Find Method Android() for Arguments
Passing Touch Events to the Parent View
How to Set Image from Url for Imageview
Does Android Volley Support Ssl
Inflateexception with Floatingactionbutton from Official Design Library