Android background thread and UI thread communication instance

This section shows you how to send data to the objects in the UI thread in the task. This feature allows you to work in the background thread and display the results in the UI thread.

Define a handler in the UI thread

Handler is a part of the thread management framework of Android system. A handler object receives the message and runs code to process the message. Normally, you can create a handler for a new thread, but you can also create a handler for an existing thread. When you connect the handler to the UI thread, the message processing code will run on the UI thread

Instantiate the handler object in the constructor of the class that creates the thread pool and save it in the global variable. Instantiate with the handler (looper) method and connect to the UI thread. The constructor uses the looper object, which is also a part of the Android system thread management framework. The looper class has a static method getmainlooper () to obtain the looper object of the UI thread. For example:

In the handler, override handlemessage(). The Android system will call this method when the thread managed by the handler receives a new message. All handler objects of a specified thread will receive the same message.

Move data from task to UI thread

To move data from the task of the background thread to the object of the UI thread, first save the reference to the UI object of the data and task object, and then pass the task object and status code to the handler object. In this object, a message containing status and task objects is sent to the handler. Because the handler runs on the UI thread, it can move data to the UI object.

Store data in task object

For example, this is a runnable running in the background thread, which parses the bitmap and saves it to its parent object. Runnable saves the status code code at the same time_ STATE_ COMPLETED。

Phototask also contains an ImageView reference to display bitmap. Although the referenced bitmap and ImageView are in the same object, you cannot directly let ImageView display bitmap because they are not in the UI thread

Send status level by level along the object hierarchy

Phototask holds the decoded data and the reference of the view object displaying the data. It receives the status code from photodecoderrunnable and transmits it along the referenced object and handler instance in the thread pool.

Move data to UI

The PhotoManager received the status code and the handle of the phototask object from the phototask object. Because the status is task_ Complete, create a message containing status and task objects and send it to the handler.

Finally, handler. Handlemessage () checks the status code for each incoming message. If the status code is task_ Complete, the task is completed. The phototask object in the message contains bitmap and ImageView. Because handler. Handlemessage () runs in the UI thread, it can safely set bitmap for ImageView

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>