I use AccessibilityService to get texts durning user making click-events on other apps.According to official documents,everything goes well. Method 1 returns true.Method 2 returns true. I meet the following problem.
The service not works when I kill the relative application manually.And the switcher still is on in system ACCESSIBILITY_SETTINGS.In this situation.Method 1 returns true.Method 2 returns false.
I turn off the switcher in system ACCESSIBILITY_SETTINGS and turn on it again,the serivce works and Method 1 Method 2 both return true.
I kill the relative application,turn off and turn on the switcher in system ACCESSIBILITY_SETTINGS,the service works.
Method 1:
private boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
// TestService为对应的服务
final String service = getPackageName() + "/" + MyAccessibilityService.class.getCanonicalName();
Log.i(TAG, "service:" + service);
// com.z.buildingaccessibilityservices/android.accessibilityservice.AccessibilityService
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);
// com.z.buildingaccessibilityservices/com.z.buildingaccessibilityservices.TestService
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;
}
Method 2:
public static boolean isStartAccessibilityService(Context context, String name){
AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
List<AccessibilityServiceInfo> serviceInfos = am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
for (AccessibilityServiceInfo info : serviceInfos) {
String id = info.getId();
if (id.contains(name)) {
return true;
}
}
return false;
}
This made user have to turn off and turn on the switcher in settings after he kill the app if he want to use the service.How to solve the problem?