Introduction to the use and principles of Android accessibility

Source: Internet
Author: User

Accessibility is a feature of Android starting from API 4, and its main purpose is to help some users who have visual, auditory, and physical disabilities to use Android without being able to use the touchscreen or ringtones completely. In fact, many developers now use it to implement some other functions, such as stealing red envelopes, automatically install APK, force stop the application and so on. Here is a brief introduction to its related use and principle.

Accessibilityservice

Its most important interface is the class Accessibilityservice. Accessibilityservice is a subclass of the service, and we can inherit this class and implement its abstract method to monitor changes in the state of an application's interface elements, such as focus changes, a button being clicked, and so on. When these changes are in place, the system wraps the information inside the accessibilityevent and callbacks the Accessibilityservice onaccessibilityevent (accessibilityevent) method. We can implement onaccessibilityevent to deal with these accessibilityevent. Here's a step-by-step example of using:

Implement Accessibilityservice

Here we use the example of converting text to speech in Apidemo, this code is/samples//apidemos/src/com/example/android/apis/accessibility/taskbackservice , how to use Apidemo can refer to samples. Here is a brief introduction to the core code:

/** * This class demonstrates how a accessibility service can query * window content to improve the feedback given to The user. */ Public  class taskbackservice extends accessibilityservice  implements  Oninitlistener {    /** Tag for logging. * *    Private Static FinalString Log_tag ="Taskbackservice/onaccessibilityevent";/** Comma Separator. * *    Private Static FinalString SEPARATOR =", ";/** The class name of tasklistview-for simplicity we speak only it items. * /    Private Static FinalString Task_list_view_class_name ="Com.example.android.apis.accessibility.TaskListView";/** Flag Whether text-to-speech is initialized. *    Private Booleanmtexttospeechinitialized;/** Handle to the Text-to-Speech engine. * *    PrivateTexttospeech MTts;@Override     Public void onserviceconnected() {//Initializes the text-to-speech engine as soon as the service is connected.MTts =NewTexttospeech (Getapplicationcontext (), This); }/** * Processes an accessibilityevent, by traversing the View's tree and * putting together a message to speak     to the user. */    @Override     Public void onaccessibilityevent(Accessibilityevent event) {if(!mtexttospeechinitialized) {LOG.E (Log_tag,"Text-to-Speech engine isn't ready. Bailing out. ");return; }//This accessibilitynodeinfo represents the view that fired the        //Accessibilityevent. The following code would use it to traverse the        //view hierarchy, using this node as a starting point.        //        //Note:every method that returns a accessibilitynodeinfo may return null,        //Because the explored window is in another process and the        //Corresponding View might is gone by the time your request reaches the        //view hierarchy.Accessibilitynodeinfo Source = Event.getsource ();if(Source = =NULL) {return; }//Grab The parent of the view that fired the event.Accessibilitynodeinfo Rownode = getlistitemnodeinfo (source);if(Rownode = =NULL) {return; }//Using This parent, get references to both child nodes, the label and the checkbox.Accessibilitynodeinfo Labelnode = Rownode.getchild (0);if(Labelnode = =NULL) {rownode.recycle ();return; } accessibilitynodeinfo Completenode = Rownode.getchild (1);if(Completenode = =NULL) {rownode.recycle ();return; }//Determine what's the task is and whether or not it's complete, based on        //The text inside the label, and the state of the Check-box.        if(Rownode.getchildcount () <2|| !rownode.getchild (1). Ischeckable ()) {rownode.recycle ();return; } charsequence Tasklabel = Labelnode.gettext ();Final BooleanIscomplete = completenode.ischecked (); String Completestr =NULL;if(Iscomplete)        {completestr = getString (R.string.task_complete); }Else{completestr = getString (R.string.task_not_complete);        } String taskstr = getString (R.string.task_complete_template, Tasklabel, COMPLETESTR); StringBuilder utterance =NewStringBuilder (TASKSTR);//The custom ListView added extra context to the event by adding an        //Accessibilityrecord to it. Extract that from the event and read it.        Final intRecords = Event.getrecordcount (); for(inti =0; i < records;            i++) {Accessibilityrecord record = Event.getrecord (i); Charsequence contentdescription = Record.getcontentdescription ();if(!                Textutils.isempty (contentdescription)) {utterance.append (SEPARATOR);            Utterance.append (contentdescription); }        }//announce the utterance.Mtts.speak (Utterance.tostring (), Texttospeech.queue_flush,NULL);    LOG.D (Log_tag, utterance.tostring ()); }PrivateAccessibilitynodeinfoGetlistitemnodeinfo(Accessibilitynodeinfo Source) {Accessibilitynodeinfo current = source; while(true) {Accessibilitynodeinfo parent = current.getparent ();if(Parent = =NULL) {return NULL; }if(Task_list_view_class_name.equals (Parent.getclassname ())) {//Find Tasklistview                returnCurrent }//Note:recycle the infos. Remember to recycle AccessibilitynodeinfoAccessibilitynodeinfo oldcurrent = current;            current = parent;        Oldcurrent.recycle (); }    }/** * {@inheritDoc} */    @Override     Public void Oninterrupt() {/* Do nothing * /}

The above code is an aid to tasklist, you can look at the Tasklistview interface:

Configuration statement for Accessibilityservice

First, as with the normal service, it needs to be declared in the manifest file:

<service  android:name  =;  <intent-filter ;  <action  android:name  = "Android.accessibilityservice.AccessibilityService" />  </intent-filter ;  <meta-data  android:name  =" Android.accessibilityservice " android:resource  = "@xml/taskbackconfig" />  </ service ;  

The

differs from the other service in that it has a metadata flag that configures the resource-taskbackconfig. The contents of the Taskbackconfig are as follows:

<accessibility-service  xmlns:android  = "http://schemas.android.com/apk/res/ Android " android:accessibilityeventtypes  =" Typeallmask " android:packagenames  =" Com.example.android.apis " android:accessibilityfeedbacktype  = "Feedbackspoken"  android:notificationtimeout  =< Span class= "Hljs-value" > "+"  android:canretrievewindowcontent  = "true"  android:description  =  "@string/accessibility_query_window_description" /> 

Where packagenames restricts the monitored package name, Accessibilityeventtype represents the event type that the Accessibilityservice wants to receive. Accessibilityfeedbacktype represents the feedback type provided, whether Canretrievewindowcontent can get the contents of the window, description represents the description information, It appears below that page when the app is allowed to be accessible in the settings in the Accessibility feature.

Brief introduction to the principle of event flow

Accessibility is the ability to get to the control's message, where is this information passed from?

Start from view

The event is associated with the view, which starts with the view, the view implements an interface called Accessibilityeventsource, and the Accessibilityeventsource interface contains two functions:

publicvoidsendAccessibilityEvent(int eventType);publicvoidsendAccessibilityEventUncheckedevent);

These two functions are used to send accessibilityevent, such as when the view receives a click event, when the focus state of the view changes, and so on, the view calls the two interfaces to turn on the send accessibilityevent.

Reverse recursive request to Viewrootimpl

In addition, ViewGroup implements the Viewparent interface, and Viewparent has an interface function requestSendAccessibilityEvent , in which the Sendaccessibilityevent method eventually calls GetParent (). Requestsendaccessibilityevent, one layer to the top of the call, and finally in Viewrootimpl inside the use of sendAccessibilityEvent Accessibilitymanager Method The binder mechanism sends accessibilityevent to Accessibilitymanagerservice.

Accessibilitymanagerservice Management Core

The Accessibilitymanagerservice is initialized in Systemserver and is added to the ServiceManager. Accessibilitymanagerservice update the Accessibilityservice bindings when there is a package change (install, uninstall), as shown in the following code:

privatevoidupdateServicesLocked(UserState userState) {   if (userState.mIsAccessibilityEnabled) {       manageServicesLocked(userState);   else {       unbindAllServicesLocked(userState);   }}
Send To Accessibilityservice

When a new accessibilityservice occurs, it binds accessibilityservice to get the result of the binding iaccessibilityserviceclient ( is actually a proxy for the iaccessibilityserviceclientwrapper returned in Accessibilityservice Onbinder), Information related to Accessibilityservice communication is stored in the Accessibilitymanagerservice.service class. When Accessibilitymanagerservice receives an event, it iterates through all the service and sends the event to Accessibilityservice through Iaccessibilityserviceclient.

This is the whole process from accessibilityevent event generation to sending to Accessibilityservice. The entire process is as follows:

Here is a brief introduction of the process, there is a chance to analyze the source.

Summarize

Accessibilityservice gives us a lot of convenience, we can use Accessibilityservice to do a lot of clever things. The main step of using Accessibilityservice is to inherit the Accessibilityservcie service, implement the Onaccessibilityevent method, configure the relevant content, Finally, the related configuration is declared in Androidmainfest. Know how to use the Accessibilityservice, it is best to understand the entire Accessibilityevent event flow, in order to be able to accessibilityservice the overall grasp.

Introduction to the use and principles of Android accessibility

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.