What is the relationship between Looper, Handler and MessageQueue in Android?

前端 未结 5 1338
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 01:00

I have checked the official Android documentation/guide for Looper, Handler and MessageQueue . But I couldn\'t get it. I am new to and

5条回答
  •  感动是毒
    2020-11-28 01:24

    MessageQueue: It is a low-level class holding the list of messages to be dispatched by a Looper. Messages are not added directly to a MessageQueue, but rather through Handler objects associated with the Looper.[3]

    Looper: It loops over a MessageQueue which contains the messages to be dispatched. The actual task of managing the queue is done by the Handler which is responsible for handling (adding, removing, dispatching) messages in the message queue.[2]

    Handler: It allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue.[4]

    When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

    Kindly, go through the below image[2] for better understanding.

提交回复
热议问题