Android asynchronous communication principle mechanism -- handler

Because the handler can communicate asynchronously, it is used in the development process. According to its usual understanding and record, some information about the organizer is written out. If there is any understanding, I hope that you can bear with me. And correct me.

1.handler is a message distribution object.

2, the role of .handler: complete the thread communication in Android (the asynchronous loading of data display, complete the time-consuming operation in the child thread, notify the UI thread to display the data after loading in the child thread)

Android asynchronous communication principle mechanism -- handler

3. The difference between AsyncTask and Handler:

The same point: complete thread communication, achieve asynchronous loading of data

The difference: AsyncTask has all the steps involved in the communication have been packaged, just remember the packaged framework, fill in the corresponding code in the framework.

If you use Handler to complete thread communication, all the steps involved need to be created by yourself.

When a program is created, a thread is created for the program. A main thread (UI thread) is created in the process, and a message queue (MessageQueue) is run. This message queue manages the highest level object in the application (four Large components) and the windows created by these objects.

4. Queue characteristics: first in, first out

5. How to achieve communication:

The handler is created in the main thread. After completion, the handler is associated with the UI thread and the MessageQueue of the UI thread. When the message is sent by the handler in the child thread, the Message object is sent to the MessageQueue of the main thread. When the MessageQueue in the main thread has a new Message entering the queue, the Looper that monitors the MessageQueue will take the Message object that has entered the queue from the queue and pass it to the Handler of the UI thread.

6.post method:

In the child thread, a Runnable object is sent by the post method. The object is added to the execution order of the main thread queue. The main thread calls the run method in the Runnable. In fact, it adds an operation to the main thread in the child thread.

postDelayed method:

In the child thread, the main thread of the network sends a message, which is delayed after the message is received by the main thread.

postAtTIme method:

The main thread will execute the Runnable object at the specified absolute time point.

7. send method

a. sendEmptyMessage method sends an empty message to the main thread

b. The sendMessage method can send an int type tag to the main thread, or a message object that has been encapsulated.

The two methods c. sendMessageDelayed and sendMessageAtTIme are similar to the two methods of post.

8.Message

Arg1 carries a simple integer

Arg2 carries a simple integer

What is the encoding of the user-defined Message object?

The obj reference type allows the Message object to carry data of the reference type (string)

When creating a Message object, it is recommended to use the Message.obtain() method instead of the new object directly.

9. Looper

Role: monitor the message queue, when it finds a new message in the message queue, the new message is taken out to the associated Handler

DP Cable

Dongguan Tuojun Electronic Technology Co., Ltd , https://www.fibercablessupplier.com

Posted on