title: Accessibility Service does not restart when manually re-enabled after app force stopped
body: <p>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 <strong>a second time.</strong>  Unfortunately apps being killed is now very common with task killers built into non-stock Android.</p>
  <p><strong>Summary:</strong></p>
  <ol>
  <li><p>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:</p>
  <pre><code>public static boolean isAccessibilityServiceEnabled(Context context) {
      String prefString = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
      return prefString != null &amp;&amp; prefString.contains(context.getPackageName());
  }
  
  </code></pre>
  </li>
  <li><p>I send the user to settings to re-enable the accessibility service. The user re-enables the accessibility service, <strong>but the accessibility service does not start again!</strong> onCreate does not run, onServiceConnected does not run, no accessibility events are received, and the following code will return false:</p>
  <pre><code>public static boolean isServiceRunning(Context context, Class targetService) {
      List&lt;ActivityManager.RunningServiceInfo&gt; serviceInfos = getRunningServices(context);
      String targetServiceName = targetService.getName();
      for (ActivityManager.RunningServiceInfo serviceInfo : serviceInfos) {
          if(serviceInfo.service.getClassName().equals(targetServiceName)) {
              return true;
          }
      }
      return false;
  }
  
  </code></pre>
  </li>
  </ol>
  <ol start="3">
  <li><p>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.</p>
  <pre><code>context.startService(new Intent(context, MyAccessibilityService.class));
  
  </code></pre>
  </li>
  <li><p>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:</p>
  <pre><code>disableSelf();
  
  </code></pre>
  </li>
  </ol>
  <p>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.</p>
  <p>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.</p>
  <p>This was tested on Android 11.</p>
  <p>Similar questions:
  <a href="https://stackoverflow.com/questions/55061568/how-to-check-if-accessibility-service-crashed-android-pie-now-displays-this-inf">How to check if accessibility service crashed (Android Pie now displays this info in settings)?</a></p>
  <p><a href="https://stackoverflow.com/questions/32157604/restart-accessibility-service-after-crash">Restart Accessibility Service After Crash</a></p>