A complete guide to Accessibility Service Part 1 — Android

Vanshika Arora
4 min readDec 25, 2018

--

Now since you are reading about accessibility service you must be familiar with the marvelous application of this service in android. If not, then I am here to guide :). The beauty of this service lies in the fact that when you grant accessibility service permission to any application then this service run in background and it can read the UI (or in more clear terms layout) of any other application.

Yes you can, not only this but also you can parse the entire UI to check what layout is there on screen (we call it root Window) or to check whether the screen has changed or the screen content has changed, or the user has performed any click action on the screen. Even we can use accessibility service to read notification coming from any application

Also this service can be used to perform action on user’s behalf like performing a click action, changing state of a checkbox, setting text into a text view without letting the user know that this is hidden job is being done by your application.

That really seems like a devil’s plan to have monopoly over any phone and into any application via accessibility service but here we are going to use only for development purposes to make applications that benefits our users. And later I’ll explain that how we are secured from any third party who could have used this service to take control over other applications in your phone.

From here an onwards we will start building an application that makes use of accessibility service.

How to build Accessibility Service?

Like any other service we need to declare this service in manifest. Here is a sample code of how we do it.

<service
android:name=".AccessibilityService"
android:enabled="true"
android:exported="true"
android:label="My application"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
>
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>

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

It’s time to explain every attribute here.

  1. Name: This refers to the name of the class which extends accessibility service.
  2. Exported:Whether or not components of other applications can invoke the service or interact with it — “true” if they can, and “false” if not. When the value is “false”, only components of the same application or applications with the same user ID can start the service or bind to it.
  3. Enabled: The attribute set to a boolean value defined in a resource file. The purpose of this attribute is to disable the provider on devices running Android 4.3 or lower.
  4. Label: The value of this attribute defines the name that will appear in the accessibility settings for this app. Setting this label to true enables the accessibility service for our application.
  5. Permission: Permission to Bind Accessibility service.
  6. Resource: This attribute in meta-data defines more about the type of accessibility actions that our application will perform. Let’s talk in detail about this.

Here is a sample code for the serviceconfig file in xml:

<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault"
android:canRequestEnhancedWebAccessibility="true"
android:notificationTimeout="100"
android:packageNames="@null"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
/>

This code determines the event types that our application will respond to, the type of feedback that application can provide the package for which this application will work, in case you want that your application should handle events arising out of some particular application and not others.

More details about each attribute can be found here.

So in this article we have talked about how to instantiate accessibility service into your android application in the next article we will be creating a class that extends AccessibilityService and we will learn how accessibility service handles events occurring on the screen. Or how it can take action on users behalf.

On reading this article I know many of you must be wondering how is this so easy for an application to read contents of another application. And take actions on users behalf. Yes, your anxiousness is justified it shouldn’t be so easy for an application to intrude into users privacy. And before you jump to any conclusion I must tell you that it is not damn easy. Although we request for accessibility permission in manifest yet it you cannot call these permissions at run-time. For enabling accessibility permissions the user must go to accessibility settings then turn on the service matching the respective label.

Since it causes security issue then why should we allow accessibility services?

Accessibility Service is equally a boon as it is a bane. It finds wide applications as it provides user interface enhancements to assist users with disabilities, or who may temporarily be unable to fully interact with a device.

For example it is beneficial for users

  1. Who are driving.
  2. Taking care of a young child.
  3. Attending a very loud party might need additional or alternative interface feedback.
  4. People with disabilities. For example: people who are visually impaired.

It is a bane at the same time as it causes privacy issue. For more questions on privacy issues. Read this.

For detailed explanation about the topic here is the next article

A Complete Guide to AccessibilityService Part 2-Android

See you in the next article….

--

--

Vanshika Arora

SDE Intern’19 @Microsoft | Mentor @Wikimedia | Android Developer | Blogger at MindOrks | Open source Contributor | Username(Github &LinkedIn)— vanshikaarora