Flexible application of Android message mechanism

Source: Internet
Author: User
 

1. Message mechanism for inter-thread Communication

1. Message Introduction

Frameworks \ base \ core \ Java \ Android \ OS \ message. Java

Message is the carrier of information transmitted between threads. It contains the Message description and any data objects. Message contains two additional int fields and an object field. In most cases, you do not need to allocate memory. Although the message constructor is public, it is best to use message. obtain () or handler. the obtainmessage () function is used to obtain the message object, because the implementation of the message contains a mechanism for recycling and reuse, which can provide efficiency.

2. Introduction to messagequeue

Frameworks \ base \ core \ Java \ Android \ OS \ messagequeue. Java

Messagequeue is used to hold message queues. The messages are distributed by logoff. Messages cannot be directly added to messagequeue, but must be added by handler associated with logoff.

3. Logoff

Frameworks \ base \ core \ Java \ Android \ OS \ lorule. Java

Logoff is used by threads to run message loops. The thread itself does not have a message loop. You need to call the perpare function in the thread and then call the loop to process the message. In Android, the system automatically creates a logoff for the main thread.

Logoff of the current thread:

Public static final void prepare ()

Logoff of the running thread:

Public static final void loop ()

Obtain the Logoff of this thread:

Public static final logoff mylogoff ()

Obtain the Logoff of the main thread:

Public synchronized static final low.getmainlow ()

4. Handler

Frameworks \ base \ core \ Java \ Android \ OS \ Handler. Java

Handler is used to send and process the message and runnable object associated with the thread. Each handler is associated with a separate thread and thread message queue. When you create a handler, the handler is bound to the thread and thread message queue. From then on, the handler can send the message and runnable to the corresponding message queue, and can process messages from the message queue.

Handler provides most of the interfaces to be called.

A. Create handler:

Create the handler associated with the local thread:

1 public Handler()
2  
3 public Handler(Callback callback)

Create the handler associated with the specified Thread:

 
1 public Handler(Looper looper)
2  
3 public Handler(Looper looper, Callback callback)

B. Create a message

1 public final
Message obtainMessage()
2  
3 public final
Message obtainMessage(int
what)
4  
5 public final
Message obtainMessage(int
what, Object obj)
6  
7 public final
Message obtainMessage(int
what, int
arg1,
int arg2)
8  
9 public final
Message obtainMessage(int
what, int
arg1,
int arg2, Object obj)

C. delete a message

Delete unprocessed messages in the Message Queue

1 public final
void removeMessages(int
what)
2  
3 public final
void removeMessages(int
what, Object object)

D. Send messages

1 public final
boolean sendMessage(Message msg)
2  
3 public final
boolean sendMessageDelayed(Message msg,
long delayMillis)
4  
5 public final
boolean post(Runnable r)
6  
7 public final
boolean postDelayed(Runnable r,
long delayMillis)

E. process messages

Cyclically retrieve the messages in the messagequeue receiving message queue from The logoff loop function, and then call the dispatchmessage function of hander to process the messages) it is specified by the user (the priority of the three methods ranges from high to low: the callback in the message, an object that implements the runnable interface, where the run function is used for processing; the mcallback in handler points to an object that implements the callback interface, where handlemessage is processed. The class corresponding to the handlemessage object for processing the message inherits and implements the handlemessage function, use the handlemessage function to process messages ).

01 public void
dispatchMessage(Message msg) {
02  
03 if (msg.callback !=
null) {
04  
05        handleCallback(msg);
06  
07     }
else {
08        if
(mCallback != null) {
09          if
(mCallback.handleMessage(msg)) {
10             return;
11          }
12        }
13  
14        handleMessage(msg);
15 }
16 }

Ii. Purpose of the Message Mechanism

The message mechanism has two purposes:

Scheduled execution: Message scheduling is executed at a specified time.

Thread communication: queue some operations in other threads for execution.

1. scheduled execution

The handler of this thread delays the message to the specified time for execution, which is equivalent to the timer function.

1 public final
boolean sendMessageDelayed(Message msg,
long delayMillis)
2  
3 public final
boolean postDelayed(Runnable r,
long delayMillis)

You can also cancel the execution through removemessages before the specified time expires.

2. Thread Communication

Android UI is single-threaded, and Android wants the UI thread to give the user a quick response. If the UI thread spends too much time doing things behind the scenes, it will take five seconds, android will give an error message. Therefore, in order to avoid dragging the UI, the time-consuming work should be handed over to independent threads for execution. However, if the background thread executes the UI object, Android sends an error message, so the UI thread and the background thread need to communicate. The UI thread distributes the work to the background thread. After the background thread executes the work, it returns the corresponding status to the UI thread so that the UI thread can update the UI accordingly.

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.