Android Message Processing

Source: Internet
Author: User

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

    1. Has a Boolean return value that determines whether the event continues to propagate down
    2. 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:

    1. Create Looper object: Looper.prepare ();
    2. 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

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.