title: Android AccessibilityService does not connect
body: <p>I'm trying to extend AccessibiltyService to intercept Notification Events.
  My problem is that my class is created by it never connects to the service.</p>
  
  <p><strong>This is how my app works:</strong></p>
  
  <ul>
  <li>There is a service (StatisticsService) that manages the collection of
  several statistics and which needs also to monitor the Notifications
  Events.  </li>
  <li>To do so it creates a StatCollector (a java object) for each
  statistic of interest.</li>
  <li>all StatCollector implement the same interface and, once created they store the StatisticService context.</li>
  <li>one of this StatCollector starts my NotificationService which extends    AccessibilityService.</li>
  </ul>
  
  <p>Following are the relevant classes:</p>
  
  <p><strong>StatCollector:</strong></p>
  
  <pre><code>public class NotificationCollector implements
          StatCollectorInterface {
  
      private Context context = null;
      private Intent notificationServiceIntent = null;
      private boolean collectorStarted = false;
  
      /**Constructor**/
      public NotificationCollector(Context context) {
          this.context = context;
          collectorStarted = false;
      }
       @Override
       public void startCollector() {
           if (collectorStarted == true)
           {
              return;
           }
               collectorStarted = true;
               notificationServiceIntent = new Intent(context,NotificationService.class);
               context.startService(notificationServiceIntent);
               }
  
        @Override
        public void stopCollector() {
          if (collectorStarted == false)
          {
              return;
          }
              collectorStarted = false;
              context.stopService(notificationServiceIntent);
              }
  </code></pre>
  
  <p><strong>NotificationService:</strong></p>
  
  <pre><code>public class NotificationService extends AccessibilityService {
      private static final Logger logger = LoggerFactory.getLogger ("Notification");
  
      @Override
      public void onCreate()
      {
              logger.trace("onCreate");
          super.onCreate();
      }
  
      @Override
      public void onDestroy()
      {
              logger.trace("onDestroy");
          super.onDestroy();
      }
  
      /**Methods from Accessibility Service**/
      @Override
      protected void onServiceConnected() {
          logger.trace("onServiceConnected");
          AccessibilityServiceInfo info = new AccessibilityServiceInfo();
          info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
          info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
          info.notificationTimeout = 100;
          setServiceInfo(info);
      }
  
      @Override
      public void onInterrupt() {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onAccessibilityEvent(AccessibilityEvent event) {
          logger.trace("onAccessibilityEvent");
          if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
              logger.trace("notification: " + event.getText());
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong>Manifest.xml:</strong> (relevant components)</p>
  
  <pre><code>&lt;service android:name=".StatisticsService"/&gt;
  
  &lt;service android:name=".service.NotificationService"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.accessibilityservice.AccessibilityService" /&gt;
       &lt;/intent-filter&gt;
       &lt;meta-data
           android:name="android.accessibilityservice"
           android:resource="@xml/accessibilityservice" /&gt;
  &lt;/service&gt;
  
  ...
  
  &lt;uses-permission android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"/&gt;
  </code></pre>
  
  <p>Note that if I add android.permission.BIND_ACCESSIBILITY_SERVICE to NotificationService the app crashes with the following error </p>
  
  <pre><code>java.lang.RuntimeException: Unable to start service my.app.StatisticsService@406657d0 
  with Intent { cmp=my.app/.StatisticsService (has extras) }: java.lang.SecurityException: 
  Not allowed to start service Intent { cmp=my.app/.NotificationService } without permission android.permission.BIND_ACCESSIBILITY_SERVICE
  </code></pre>
  
  <p>I believe that the problem is that the System is ignoring my NotificationService class since it doesn't explicitly mention that it has BIND_ACCESSIBILITY_SERVICE permission. But if I add it to the service in the manifest file the app crashes.</p>
  
  <p>What am I missing/doing wrong?</p>
  
  <p>Thanks</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thanks to the comment of <a href="https://stackoverflow.com/users/1381157/dr-cyanide">Dr.Cyanide</a> I found out that Accessibility should be enabled for a certain app by the user in the Accessibility menu under settings. </p>
  
  <p>Furthermore, the tag </p>
  
  <pre><code>&lt;uses-permission android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"/&gt;
  </code></pre>
  
  <p>is not necessary for pre jelly-bean versions so I've removed it since I'm using ICS. </p>
  
  <p>Now the app works exactly as expected</p>