The main contents of this chapter include:
One, the Android event processing mechanism, including the monitoring and callback two processing mechanisms
Second, the configuration class
Third, Handler, Loop and MessageQueue
Iv. Asynctask
I. Monitoring mechanism and callback mechanism
1.1-Monitoring Mechanism
The Listener event processing model consists of three classes of objects: event sources, events, and event listeners. Where the event source is the place/component where the event occurred, the event is a user action, and the event listener is responsible for listening and handling events.
As an example of the process of displaying a toast in the main interface after the user taps a button, where:
Event Source is button
Event for the user to click on the button
The event Listener (Onclicklistener here) is responsible for listening and handling the event (display toast)
A typical code implementation:
Btn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {toast.maketext (Getapplication (), "Btn Clicked", Toast.length_short). Show ();}});
There are four main types of listener implementations: internal class form, external class form, activity itself as event listener class, Anonymous inner class form
The implementation in code is in the form of an anonymous inner class, which is also the most common form.
1.2-Callback mechanism
The main difference between the callback and the listening mechanism is that the listener is responsible for monitoring and handling the events that occur. In the callback mechanism, the event source has already written what should be handled when the event occurs, that is, the action to handle the event is done by the event source itself, rather than being entrusted to the event listener. After you overload the callback function, place the view in the activity's layout file.
Note that the return value of the callback function here is Boolean. Returns true to indicate that the event processing does not pass up after completion. Returns false, which means that the event will continue to be passed to a higher level for processing.
Similarly, with the click button as an example, if False is returned in the button, after the event is processed in the callback function of the button, the response callback function in the activity that placed the button continues to be called for processing.
Code implementation:
public class MyButton extends Button {public MyButton (context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor stub} @Overridepublic Boolean Ontouchevent (Motionevent e) {toast.maketext (GetContext (), " Btn Clicked ", Toast.length_short). Show (); return true;}}
If both the listening and callback mechanisms are implemented, the call sequence is a callback called the listener. For example, if both are implemented, the Onclicklistener in the listener is called first, and then the Ontouchevent is recalled.
Second, the configuration class: The class used to get the state of the device, Android can call the activity of the following methods to obtain the System configuration object:
Configuration cfg = getresource (). GetConfiguration ();
Third, Handler, Loop and MessageQueue
Child threads are not allowed to perform UI operations in Android, only by using handler.
The main role of Handler is two:
1. Sending messages in a newly-started thread
2. Get and process messages in the main thread
The messages processed by handler originate from MessageQueue, and MessageQueue is managed by loop.
The process by which a child thread needs to perform a UI action is when a child thread adds a message request to the Cheng thread's MessageQueue at the end of MessageQueue, and then gets the message back to the Handlemessage function in the thread to perform the processing action.
To implement a child threading class with UI operational requirements, you need to write the following steps in public void Run ():
1. Call Looper.prepare (); Create the MessageQueue of the thread
2. Define handler, overload the Handlemessage function. The main line Cheng the thread's MessageQueue after the message is removed and the function executes the UI action
3.looper.loop (); Start Looper.
The system has initialized a Looper object in the main UI thread, so there is no need to execute Looper.prepare,looper.loop ()
Iv. Asynctask
Slightly
Crazy Android Handout Chapter III-android event handling