Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart Accessibility Service After Crash

In my Application there is a Service and an AccessibilityService. Now the Service is bound by the accessibility Service in order to receive information.

My Service periodically checks if the AccessibilityService is enabled, and if it is not, it sends a notification to the user that it has to enable it.

Once enabled, AccessibilityService starts to work. First it binds to my Service, and after, it begins to send information.

The problem is if AccessibilityService crashes. It remains enabled but there is no running instance. So my Service finds it enabled but actually the AccessibilityService is not running.

How Check AccessibilityService

public boolean checkAccesibilityService()
    {
        int accessibilityEnabled = 0;
        boolean accessibilityFound = false;
        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());
        }

        if (accessibilityEnabled == 1) {
            //Log.v(TAG, "***ACCESSIBILIY IS ENABLED*** -----------------");
            String settingValue = Settings.Secure.getString(
                    mContext.getApplicationContext().getContentResolver(),
                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null) {

                if(TextUtils.isEmpty(settingValue)) {
                    Log.i(TAG, "Nessun servizio abilitato!");
                    return false;
                }
                TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(':');
                splitter.setString(settingValue);

                while (splitter.hasNext()) {
                    String accessabilityService = splitter.next();

                    //Log.v(TAG, "-------------- > accessabilityService :: " + accessabilityService);
                    if (accessabilityService.equalsIgnoreCase(ACCESSIBILITY_SERVCE)) {
                        //Log.v(TAG, "Accessibility Service Attivato!");
                        return true;
                    }
                }
                Log.v(TAG, "Accesibility Service non abilitato!");
                return false;
            }
        } else {
            Log.v(TAG, "***ACCESSIBILIY IS DISABLED***");
        }

        return accessibilityFound;

    }

Q1: Why when the AccessibilityService crashes does the Android OS not restart it?

Q2: Can I change my code to check if the AccessibilityService is enabled but also has a running instance?

like image 640
paolo2988 Avatar asked Aug 22 '15 15:08

paolo2988


People also ask

Why is my accessibility not working?

Launch the "Settings" app from the All Apps screen, then choose "Accessibility." Make sure the options you require are all enabled. Select "Enhance Web Accessibility," for example, to allow Google websites to install browser scripts to aid accessibility.

What is an accessibility service on Android?

An Accessibility Service assists users with disabilities in using Android devices and apps. It is a long-running privileged service that helps users process information on the screen and lets them to interact meaningfully with a device.


1 Answers

Why when the AccessibilityService crashes does the Android OS not restart it?

I don't know, but I suspect there isn't much you can do about this. I've had similar problems with AccessibilityServices, so I think this is normal behaviour. The best I can think of is:

  • Carefully write your AccessibilityService code and thoroughly test it so that it doesn't contain any bugs that will cause it to crash.
  • Don't do any logic in your AccessibilityService that you could also do in a different place. The less work your AccessibilityService is doing, the simpler it is, so the less likely it is to have bugs.
  • Make your AccessibilityService code error-tolerant. Predict errors that will happen inside the service, and write code to catch them and recover from them so that the AccessibilityService continues running normally if possible.
  • Run your AccessibilityService in its own process so it isn't killed when your other code crashes. Note that this might be difficult depending on how the service interacts with the rest of your code.

Can I change my code to check if the AccessibilityService is enabled but also has a running instance?

Yes; there are a couple of common techniques to do this. See How to check if a service is running on Android?.

like image 69
Sam Avatar answered Sep 18 '22 16:09

Sam