16

I'm trying to start my implementation of AccessibilityService by using

Intent mailAccessabilityIntent = new Intent(this, EmailAccessabilityService.class);
startService(mailAccessabilityIntent);

My problem is onServiceConnected() never been called. How do i start this service properly?

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
eyal
  • 2,379
  • 7
  • 40
  • 54

2 Answers2

40

Because accessibility services are able to explore and interact with on-screen content, a user has to explicitly enable services in Settings > Accessibility. Once a service is enabled, the system will start it automatically and bind it to the accessibility APIs.

Make sure you declare your service in your application manifest:

<service android:name=".MyAccessibilityService"
         android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
     <intent-filter>
         <action android:name="android.accessibilityservice.AccessibilityService" />
     </intent-filter>
     . . .
 </service>

You'll also need to provide configuration for your service, either by overriding setServiceInfo(AccessibilityServiceInfo) or adding a meta-data attribute and XML config file.

The meta-data attribute goes in your <service> declaration after the <intent-filter> tag and looks like this:

<meta-data android:name="android.accessibilityservice"
           android:resource="@xml/accessibilityservice" />

The XML config that you're referencing (in this case, accessibilityservice.xml) looks like this:

<accessibility-service
    android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
    android:packageNames="foo.bar, foo.baz"
    android:accessibilityFeedbackType="feedbackSpoken"
    android:notificationTimeout="100"
    android:accessibilityFlags="flagDefault"
    android:settingsActivity="foo.bar.TestBackActivity"
    android:canRetrieveWindowContent="true"
    . . .
/>

There's more information on which tags you can use at http://developer.android.com/reference/android/R.styleable.html#AccessibilityService

alanv
  • 23,966
  • 4
  • 93
  • 80
  • 1
    you really deserve a lot of upvotes. Because even the documentation is wrong at http://developer.android.com/guide/topics/ui/accessibility/services.html. You have shown the right way to make an accessibility service – gaurav414u Apr 20 '15 at 08:18
  • 1
    @alanv "Once a service is enabled, the system will start it automatically and bind it to the accessibility APIs." I have question, my Accessibility Service once enabled work. Bit when it crashes it results enable in tue setting but it does not result in the execution service. Why System does not restart my service after a crash but it still result enabled? – paolo2988 Aug 21 '15 at 23:54
  • 4
    @v1sc3rr4k Once a system-linked service (such as Accessibility Service) crashes, it is not restarted by the system until the permission is reseted or the OS is rebooted. Try to revoke the permission and enable it again, the service will start again. Hope it helps. – abhi Nov 30 '15 at 23:50
  • @eyal if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Sufian May 29 '17 at 10:00
  • Can you please tell, how we can pass data to Accessibility service from activity @alanv ? – Raj Dec 13 '19 at 19:09
1

I've just done this today. Once your service is created and is correctly listed in the manifest file with the accessibility intent filter. Then yours service will appear under settings-accessibility-services. You start it by clicking on it then toggling it on. I don't know how to start it via intents yet if its even possible. I think its Lee GPS you can only navigate users to start button.

JonWillis
  • 3,146
  • 5
  • 32
  • 54