Restart Accessibility Service After Crash

不羁岁月 提交于 2019-12-23 12:55:31

问题


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?


回答1:


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?.




回答2:


You can turn off the Accessibility Service programmatically just before crash occurrs in handleUncaughtException ( Application Class ) by calling disableSelf(); method
But this works on Nogout and above only.



来源:https://stackoverflow.com/questions/32157604/restart-accessibility-service-after-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!