6

I'm having an issue with my Accessibility Service not restarting after the app is force stopped and then manually opened again, which does restart other regular services. The accessibility service does not start up and work again until it is manually re-enabled (expected), disabled, and re-enabled a second time. Unfortunately apps being killed is now very common with task killers built into non-stock Android.

Summary:

  1. Force stopping the app disables the Accessibility Service. This is expected. I can detect that the accessibility service is no longer enabled whenever the app is manually opened next with this code:

    public static boolean isAccessibilityServiceEnabled(Context context) {
        String prefString = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
        return prefString != null && prefString.contains(context.getPackageName());
    }
    
    
  2. I send the user to settings to re-enable the accessibility service. The user re-enables the accessibility service, but the accessibility service does not start again! onCreate does not run, onServiceConnected does not run, no accessibility events are received, and the following code will return false:

    public static boolean isServiceRunning(Context context, Class targetService) {
        List<ActivityManager.RunningServiceInfo> serviceInfos = getRunningServices(context);
        String targetServiceName = targetService.getName();
        for (ActivityManager.RunningServiceInfo serviceInfo : serviceInfos) {
            if(serviceInfo.service.getClassName().equals(targetServiceName)) {
                return true;
            }
        }
        return false;
    }
    
    
  1. I tried manually starting up the service with the code below. This actually triggers onCreate and onStartCommand, but not onServiceConnected, and no accessibility events are received.

    context.startService(new Intent(context, MyAccessibilityService.class));
    
    
  2. I tried to disable the service after starting it manually above, so that the user is forced to re-enable it a second time (which would fix it), but this does not work:

    disableSelf();
    
    

In summary, the user thinks they have re-enabled the service, but it does not work. Restarting the device or disabling the service and re-enabling it a second time does fix it.

Anyone know how to get around this? It's bad enough that the service is disabled when the app is force stopped, but it's even worse that it does not start even when manually re-enabled by the user.

This was tested on Android 11.

Similar questions: How to check if accessibility service crashed (Android Pie now displays this info in settings)?

Restart Accessibility Service After Crash

Flyview
  • 1,899
  • 1
  • 28
  • 46

2 Answers2

0

The only way I have been able to reliably start my AccessibilityService (without a shortcut assigned) is through the ADB:

#!/bin/sh
VALUE_OFF="com.android.talkback/com.google.android.marvin.talkback.TalkBackService"
TALKBACK="com.google.android.marvin.talkback/com.google.android.marvin.talkback.TalkBackService"
ALLYSERVICE="[myservicename]"
VALUE_ON="$TALKBACK:$ALLYSERVICE"

if [ "$ENABLE" = true ]; then
  adb shell settings put secure enabled_accessibility_services $VALUE_ON
  adb shell settings put secure accessibility_enabled 1
else
  adb shell settings put secure enabled_accessibility_services $VALUE_OFF
  adb shell settings put secure accessibility_enabled 0
fi

But it seems to be a system thing. Have you raised an issue on the issue tracker?

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
  • 1
    Thanks for the answer. Well, I need this to run reliably on client phones, so adb is unfortunately not a good solution. I could raise an issue on the issue tracker, as I have in the past, and watch nothing happen for years until they mark it as "obsolete" and ask me to re-log it. Useless. – Flyview Jun 04 '21 at 22:08
  • @Flyview yeah they do take a long time. And I noticed that it’s a 4 year old problem. Do you have links to the issue tracker? The accessibility team (not the developers) are fairly friendly – Quintin Balsdon Jun 05 '21 at 11:43
0

I had the same problem, I fixed it by rebooting my phone (reboot emulator if you using an emulator). I think there is a bug in the android accessibility code.