Handler mechanism (a) - Handler implementation process

Because Android uses a single-threaded mode, developers can not update the UI in the sub-thread, the system gave me Handler class to implement this UI update issues. This paste is mainly explained Handler workflow.

1. Handler's role

Android in order to protect the security thread, updating the UI information can only be prescribed by the main thread. In the actual development, the child will often encounter multiple threads to manipulate the situation UI information, it will cause the UI thread safe. At this point, we need the help of Handler as a medium, let Handler notify the main thread one by one in order to update the UI, the UI thread to avoid unsafe.

Then, when the child thread to update the UI, we need to be updated message to UIthe main thread, then the main thread has finished updating, in order to achieve the working threads UIupdate processing, the final completion of the asynchronous message processing (FIG. 1 shown).

 

Figure 1 - a messaging flow,

 

 2. Handler concepts explained

 Primarily involved are: Processor (Handler), message (Message), the message queue (Message Queue), a circulator (Looper)

concept

definition

effect

Remark

The main thread

(Main Thread)

When the application first starts,

It will open a main thread.

UI-related processing events

The main thread

Communication media: Handler

Child thread

Child thread Human hand open

Perform time-consuming operations

(E.g., network request, etc.)

News

(Message)

Inter-thread communication data unit

(I.e. Handler receiving / processing object)

Information storage needs operation  

message queue

(Message Queue)

A data structure

(Team: First In First Out)

Storing messages sent by Handler

(Message)

 

Processor

(Handler)

◆ main thread and the child thread communication medium;

◆ message handler thread.           

◆ Add a message (Message) to the message queue (Message Queue);

◆ Processing message (Message) assigned by the circulator (Looper) over. 

 

Circulator

(Looper)

Message Queue (Message Queue) and the communication medium Handler

That is the message loop:

◆ 消息获取:循环取出消息队列(Message Queue)中的消息(Message);

◆ 消息分发:将取出的消息(Message)发送给对应的处理者(Handler)。

 

 

3. 工作原理及流程

Handler 机制流程主要包含4个步骤:

        ① 异步通讯;

        ② 消息发送;

        ③ 消息循环;

        ④ 消息处理。

详情如下表所示:

步骤 具体描述 备注
① 异步通讯

在主线程中创建:

◆ 处理器对象(Looper)

◆ 消息队列对象(Message Queue)

◆ Handler对象

◆ Looper、Message Queue均属于主线程;

◆ 创建完Message Queue后,Looper自动进行消息循环;

◆ 此时,Handler自动绑定到主线程的Looper和Message Queue。

② 消息发送 子线程通过Handler发送消息消息(Message)到消息队列(Message Queue)中。 消息内容 =  子线程对UI的操作
③ 消息循环

◆ 消息出队:Looper从消息队列(Message Queue)中循环取出消息(Message);

◆ 消息分发:Looper将取出的消息(Message)分发给消息的处理者(Handler)。

在消息循环过程中,如果消息队列为空,则自动阻塞。
④ 消息处理

◆ 处理者(Handler)接受到处理器(Looper)发送过来的消息(Message);

◆ 处理者(Handler)根据消息(Message)进行处理。

 

 

工作流图(如图2所示):

 

图2 - Handler 机制工作流图

 

4. 总结

最后需要注意几点:

线程(Thread)、循环器(Looper)、处理器(Handler)之间的关系如下:

  • 一个线程(Thread)只能绑定一个循环器(Looper);但一个Thread可以有多个处理器(Handler)。
  • 一个循环器(Looper)可绑定多个处理器(Handler)。
  • 一个处理器(Handler)只能绑定一个循环器(Looper)。

 

图3 - Thread、Looper、Handler 之间的关系

 

Guess you like

Origin www.cnblogs.com/steffen/p/11243489.html