Bind to Accessibility Service in AndroidSep 18th 2020Words: 294
This post is created a year ago, the content may be outdated.
Background
I’m building an Android app that simulates user input. Accessibility is a great non-root solution thanks to continued gesture introduced in API 26.
However, the lifecycle of the accessibility service is almost controlled by system alone, and onAccessibilityEvent seems to be the only place to implement the workflow. This may be enough for apps that simply does “find a button in the view and click it”, but for my app, where all the logic is implemented somewhere else, I have to communicate to accessibility service in another component. For example, one activity sends the commands “tap at (123,456) for me”, and my accessibility should executes this.
Solution
My solution is to use static variable to access the instance of accessibility service, although it is not elegant.
Now add an auxillary service named MyService and use it as a proxy to access methods in MyAccessibilityService, now you you can do anything to MyService, for example, implement AIDL to bind it remotely.