Accessibility services

“This is the 9th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Accessibility services are applications that provide interface enhancements to assist users with disabilities or who may be temporarily unable to fully interact with the device.

Creating accessibility services

Accessibility services can be bundled with regular apps or created as a separate Android project. In either case, the steps to create the service are the same. In the project, create a class that extends AccessibilityService.

package com.example.android.apis.accessibility;
import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityEvent;
public class MyAccessibilityService extends AccessibilityService {...@Override
    public void onAccessibilityEvent(AccessibilityEvent event) {}@Override
    public void onInterrupt(a) {}... }Copy the code

If you create a new project for the service and do not intend to associate any applications with the service, you can remove the launcher Activity class from the source code.

Manifest declarations and permissions

Apps that provide accessibility services must include specific statements in their app listings in order to be considered accessible by the Android system. This section describes the required and optional Settings for accessibility services.

Accessibility Statement:

The accessibility service declaration must be in the application element (not the Activity element) of the manifest. In addition, within the Service element, you must add a barrier-free Service Intent filter. To be compatible with Android 4.1 and later, the manifest must also protect the service by adding the BIND_ACCESSIBILITY_SERVICE privilege to ensure that only the system can bind to it. The following is an example:

<application>
    <service android:name=".MyAccessibilityService"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:label="@string/accessibility_service_label">
      <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
      </intent-filter>
    </service>
  </application>
Copy the code

Accessibility Configuration

Accessibility services must also provide configurations that specify the types of accessibility events that the service can handle and other information about the service. There are two ways to configure accessibility services :(1) the accessibility service configuration is contained in the AccessibilityServiceInfo class. Your service can be used at runtime such instances and setServiceInfo (android. Accessibilityservice. AccessibilityServiceInfo) build and set the configuration. However, not all configuration options are available when using this approach:

@Override
public void onServiceConnected(a) {
    // Sets the type of event this service listens for. Others will not be passed to the service.
    info.eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED |
            AccessibilityEvent.TYPE_VIEW_FOCUSED;
    // If you only want this service to work with a specific application, set it

    // Here is the package name. Otherwise, when the service is activated, it will listen

    // Send events from all applications.
    info.packageNames = new String[]
            {"com.example.android.myFirstApp"."com.example.android.mySecondApp"};
    // Set the type of feedback your service will provide.
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
    // The default service is invoked only if no package-specific service exists

    // The type of AccessibilityEvent to be generated. This service is *

    // Application-specific, so this flag is not required. If this is a

    // For generic services, you should consider setting the default flag.
    // info.flags = AccessibilityServiceInfo.DEFAULT;
    info.notificationTimeout = 100;
    this.setServiceInfo(info);
}
Copy the code

The second way is to configure the service using XML files. Some configuration options, such as canRetrieveWindowContent, are only available when you configure the service using XML. Starting with Android 4.0, you can add an element to the manifest that references the configuration file, which allows you to set all options for accessibility services, as shown in the following example:

<service android:name=".MyAccessibilityService">... <meta-data android:name="android.accessibilityservice"
    android:resource="@xml/accessibility_service_config" />
</service>
Copy the code

This metadata element refers to an XML file (/res/ XML/Accessibility_service_config.xml) that you created in your application’s resource directory. The following code shows the sample contents of the service configuration file:

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/accessibility_service_description"
    android:packageNames="com.example.android.apis"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFlags="flagDefault"
    android:accessibilityFeedbackType="feedbackSpoken"
    android:notificationTimeout="100"
    android:canRetrieveWindowContent="true"
    android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"
/>
Copy the code

android:accessibilityEventTypes=”typeAllMask”

Used to set the type of event to respond to, typeAllMask of course is to respond to all types of events. Of course there are clicks, long presses, swipes and so on.

android:accessibilityFeedbackType=”feedbackSpoken”

Set the feedback mode to the user, including voice broadcast and vibration. Some TTS engines can be configured to implement pronunciation.

android:notificationTimeout=”100″

Response time Settings

android:packageNames=”com.example.android.apis”

You can specify events that respond to one application, but the default is to respond to all applications.