Detailed explanation of Handler mechanism

Handler

  • Handler:

    • Overview: Handler is responsible for fetching, sending and processing Messages. Each Handler instance is associated with the thread that
      created it and the thread's MessageQueue. The default Handler is synchronous, or you can
      extend android.os.Handler to construct an asynchronous Handler, that is, send messages asynchronously.
    • effect:

      • Function 1: Send a message or Runnable to send a message or Runnable at some point in the future
        • post: Sorts the Runnable to be called by the MessageQueue when it is received.
        • sendMessage: Sorts messages containing Bundle data to be called by the handlerMessage
          method.
      • Function 2: Thread switching The action (activity) enqueue (queuing) is executed in other threads.
    • Create Handler:

      • Method 1: Implement Callback: The Handler created by implementing Callback uses the Looper of the current
        thread (tested).
      • Other methods: see Handler's constructor.
    • Quantity: Multiple Handlers can be instantiated in a thread, but only one Looper associated with it can be instantiated.
      It has been tested that in the main thread, the Handlers created by extending Handler and implementing the Callback method
      have different addresses in memory, but the addresses in memory of Looper obtained through these two Handlers are the
      same.

  • Looper:

    • Overview: A class that runs message loops for threads. Threads are not associated with Looper by default. To create a Looper
      , first mobilize looper.prepare() where the thread runs the loop, then call looper.loop() to process the
      message , and then call looper. When prepare() (that is, creating a Looper), a MessageQueue will be created, the
      Looper is responsible for looping, and the MessageQueue is responsible for taking out messages, and all Messages are continuously taken
      out .
    • Message scheduling: dispatchMessage(msg) will be executed in the Looper.loop() method to schedule
      messages .
  • MessageQueue:

    • Overview: Message is not directly added to the MessageQueue, but through the
      Handler associated with Looper.
  • Handler mechanism summary:

    • Handler working mechanism: Looper is a message loop that continuously
      takes out Messages through MessageQueue, sends Messages to the outside through the Handler associated with the Looper, and
      processes the Messages through the Handler's Callback callback.
  • Advantages and disadvantages: (taken from the Internet)

    • Pros: (feels like crap, didn't think so)
      • Clear structure and clearly defined functions
      • Simple and clear for multiple background tasks
    • Disadvantages: (also feel like nonsense)
      • When a single background is asynchronously processed, it appears that the code is too much and the structure is too complex (relativity)
  • Demo download address:

https://download.csdn.net/download/chenyuan_jhon/10349121

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324407002&siteId=291194637