Android two modes of event handling
Listener-based event handling
Event Source events sources, event events, event listeners Listener,
Event sources delegate events to event listeners, which are handled by event listeners in response to events.
Setxxxlistener ()
Callback-based event handling
- Has a Boolean return value that determines whether the event continues to propagate down
- Extends Button/activity/view
Methods for implementing the response component, for example: OnKeyDown (), Ontouchevent ()
Drawing the appropriate component in the layout file
System Setup class configuration, Method Onconfigurationchanged () is a callback-based event processing method, which is used to monitor the changes of system settings;
Handler Message passing mechanism
Handler has two main functions: sending a message in a newly-started thread, and fetching and processing messages in the main thread.
Looper: Each thread can produce a Looper that is used to manage the message Queue and remove messages from MessageQueue
Handler: Constructs a Handler object to communicate with Looper in order to push a new message into the message queue, or to receive messages sent from the message queue by Looper.
Message Queue: Manage message
Handler execution Process:
Steps to use handler:
- Create Looper object: Looper.prepare ();
- Create an instance of the handler subclass, overriding the Handlemessage () method;
Mhandler = new Handler () {
Handlemessage ();
}
3. Call the Looper.loop () method to start the loop.
4. Instantiate the new Looper thread in the UI thread and call Thread.run () to start the thread;
The UI thread sends a message to the handler in the new Thread Handler.sendmessage ()
Android Message Processing