Android system input Event distribution detailed

Source: Internet
Author: User

What is an input event?

we know that running Android the device of the system is essentially a computer, which can be abstracted into a simple input and output to the computer when interacting with the computer ( IO ). Then for the operating system running on the computer, the operating system in the interaction with the user when the start can also be abstracted to the outside of the input processing, and then return the output to the user. This article only discusses "input Events" in the Android system (because this article is all about input events, so the following is referred to as "input events" as "events").

Based on the above description, we can answer the above questions:

An event is the input information received by a computer device.

With the above definitions, let's start by examining how the events are generated.

in the Android Development

In the system, events are triggered by the user's actions, which in turn Android hardware layer causes changes in the hardware state, Android The bottom layer encapsulates the changes in the state of the hardware into more advanced class objects and passes them to Android of the Framework layer, Framework will continue to pass the event to the application layer (specifically the application developer-written View sub-class object), the final application layer responds to the event and responds to the user through a call to the hardware. Throughout the process, the application developer only needs to focus on a few of them, that is, how events are handled after the event passes from the Framework layer to the application layer. Of course, this article also mainly discusses these contents.

borrow the following Android frame composition to show how many processes have been experienced from the bottom to the application layer. android architecture diagram

Note: Event delivery is not just a single event, and each time a user's action produces an ordered list of events, the ordered event column is passed to the application layer in order.

Input Event Type

Android all the events in the event are abstracted into a Java class, inputevent . It has two direct subclasses,motionevent and keyevent, which represent events that aregenerated by events and keystrokes that are usually generated by the touch screen and are the most common. But in fact inputevent not only have these two kinds. Because the android system can run a very rich type of device, on different devices corresponding to different hardware, there must be a different input events,inputevent Depending on the input source of the event, it can be divided into the following categories (not all):

Sensor inputevent ( Transducer input Event )

Hook inputevent ( headset hook Key input event )

On inputevent ( boot key input event )

Touch screen inputevent ( touchscreen input event, which is the main introduction of this article )

key InputEvent ( key input event,home/menu/back, TV Remote control button, etc. )

Hover inputevent ( mouse Action input event )

in general, Android the system's operating devices are mobile phones or tablets, they will have a touch screen, the most common input events or Touch Screen InputEvent up. This article will mainly introduce touchscreen inputevent and key inputevent. android system input Device list reference here: Input device

Touch Screen InputEvent the processing and delivery

Touch Screen InputEvent Basic

Touch Screen InputEvent is encapsulated as a Java Class- motionevent , it contains three basic events, and other events are packaged together on the basis of three events, for example, Click event is a Action_down and a action_up the combination.

Base event:

Action_down press the finger on the screen

Action_move finger moves on the screen

action_up finger away from the screen

Sequence of events:

above and mentioned, Touch Screen event is not a single occurrence, each interaction of the user produces an event sequence that is Action_down beginning, in the middle there 0 or N a Action_move events, and finally with a action_up end. An event sequence can often compose more advanced events, such as click Events, double-click events, gesture events, and so on, and theView will consume these events with a separate sequence of events as a minimum unit for consumption, rather than a single base event.

So, we can recognize that Touch Screen The events can be divided into two types:

underlying type: contains Action_down , Action_move , action_up three of them.

Composite type: An event that represents a user-specific input information by combining an event sequence of the underlying type.

Several key methods and their functions

in software development, when we write the next method, our goal may be to use this method to accomplish certain functions, and possibly for other reasons such as the extensibility of the program. For example, in the common Android component Activity class, there is a method called Setcontentview (), The purpose of this is to assign a layout to the current Activity, which is the method we have just mentioned, "to accomplish certain functions";there's another one in theactivity called the onCreate () method, which functions as a placeholder method, indicates that the Activity is now in the process of being created, You can override this method in subclasses to do some initialization work, such as this kind of method does not complete some specific functions, that is, we mentioned "for the sake of the scalability of the program and other reasons." For the sake of convenience, in this article I will call the first method "functional method", called the second type "structural method". However, some methods are not simply identified as a type, it is possible that both of these attributes are included. as an example of the onCreate method of Activity mentioned above, first of all, it is a structured method, because it is intended to include notification of initialization of subclasses, and secondly, It also carries out a part of the initialization of the Activity , so that he also accomplishes some functions, or a functional approach, in fact onCreate Both are ( hereinafter referred to as the class method Both).

in the motionevent There are several key methods in the process of handling and transmitting, they appear separately View , ViewGroup , Activity , Window classes, but the names are exactly the same and the functions are very close.

Some of the key methods mentioned above are motionevent the key to event distribution and processing involves several classes:

Activity

Window

View

ViewGroup

So, motionevent How are events passed between these classes? And we know that the event is passed from the framework layer to the application layer, so who is passing it from the framework layer and who is receiving it at the application level?

by viewing Android SDK The source code, can be found to be Windowmanagerservice this key class puts events from Framework layer is passed to the application layer, and the Phonewindow members of Decorview is passed to the current Activity (for details, please refer to the Android FrameWork -- Touch Detailed event Distribution process , This article does not repeat). That is to say , Windowmanagerservice made a bridge between the Framework layer and the application layer, The Motionevent event is passed to the current activity, which is where the application layer first receives motionevent the class of the event.

at this time, we can understand why Activity in a dispatchtouchevent and the ontouchevent these two classes, because Activity is the first stop for application-level event distribution.

we've learned that. motionevent How did it get through Activity passed to View in, and Android of the View a layer is a tree-like structure. View tree, each node of the tree is also a View , then motionevent is how to View what about the inside of the tree?

first, the root associated with the event View of the dispatchtouchevent will be called once to come in the View The event is distributed to the class's ontouchevent method, the View of the Ontouchlistener of the OnTouch methods, and sub - View of the dispatchtouchevent method. View will feel the final return value of the Dispatchtouchevent based on the return value of the three target methods mentioned above .

then, when the event is distributed to the ontouchevent method, the View the event will be processed, View of the Onclicklistener of the OnClick The method is called here.

If it is said that View is a ViewGroup , then it's onintercepttouchevent method is dispatchtouchevent method is invoked at the time of execution, and it is considered as whether to interrupt event passing dispatchtouchevent 's return value.

The following pseudo-code can clearly show the relationship of the three of them:

Java

Public booleandispatchtouchevent (Montionevent e) {

Boolean consume = false;

if (Onintercepttouchevent (e)) {

consume = Ontouchevent (e);

}else{

consume = Child.dispatchtouchevent (e);

}

return consume;

}

Public booleandispatchtouchevent (Montionevent e) {

Boolean consume = false;

if (Onintercepttouchevent (e)) {

consume = Ontouchevent (e);

}else{

consume = Child.dispatchtouchevent (e);

}

return consume;

}

of course, actually. View in the dispatchtouchevent to be more complex, it needs to deal with a lot of things, here we just pick the main introduction.

motionevent The sequence of events is treated as a whole, if a View no consumption of a Action_down event, the subsequent events of that sequence will not be passed to it.

when a View If you feel that you want to process a time series, all the child events of the sequence of events will be given to it for processing.

ViewGroup of the onintercepttouchevent Default return false , which means ViewGroup the default is to not intercept events.

Key inputevent the processing and delivery

Key inputevent Basic

at the application level, Key inputevent is encapsulated in a form called KeyEvent the class that it wants to motionevent as well as inputevent a subclass of the class. In general,KeyEvent is consistent with Motion Event delivery in the application layer . are obtained by Windowmanagerservice to an event, called by decorview viewroot , and then follow Activity - Window - View The process of the tree passes events.

similar to motionevent the key methods, KeyEvent the key methods are:

Dispatchkeyevent

Onkeyevent

Oninterceptkeyevent

in the View the transfer process in the tree Touch The incident is consistent, not to repeat here.

the above mentioned is only Android Development

In the general outline of the event distribution mechanism, the specific code is more complex, we have time to study the specific source, I believe we will have a great harvest.



Android system input Event distribution detailed

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.