Several methods of sub thread operating UI in Android Development

Such problems are often encountered in Android projects. After time-consuming operations are completed in the sub thread, the UI needs to be updated. The following is a summary of some projects I have experienced:

Before looking at the method, you need to understand the message mechanism in Android.

Method 1 activity runOnUiThread

The method is as follows:

This method is simple and easy to use. If the current thread is a UI thread, the action is executed immediately. If the current thread is not a UI thread, it is published to the UI thread of the event queue. In fact, it is similar to the handler. It adds the request message for updating the UI to the event queue and waits for the main thread to execute when it is idle.

Method 2 handler

The handler defined in the main thread is as follows:

The sub thread sends a message to notify the handler to complete UI update. The code is as follows:

Method 3 view post

The above code is to update the content in BTN. Similarly, the following code can achieve this effect.

This is using handler Post method, one is to use view Post method, handler The post method has been introduced in the message mechanism of Android. In fact, it finally calls the send method in method 2.

Now take a look at view Source code of post method:

The main function code in the method is attachinfo Mhandler, get the hanlder of the current thread (i.e. UI thread), and then post the action object to the handler. The processing process in the handler has been clearly analyzed in the link above. It wraps the passed action object into a message (the callback of the message is action), and then puts it into the message loop of the UI thread. In the dispatchmessage method of the handler, the first sentence is set for it to directly call the run method of runnable. At this time, it has been routed to the UI thread, so we can update the UI without worry.

Method 4 broadcast

The child thread sends the broadcast, and the main thread receives the broadcast and updates the UI.

Method 5 use asynctask

In order to simplify access to the UI in the child thread, the system provides us with asynctask.

Asynctask is a lightweight asynchronous task class. It can execute background tasks in the thread pool, then pass the execution progress and results to the main thread and update the UI. In essence, asynctask encapsulates thread and handler, but asynctask is not suitable for background tasks that are particularly time-consuming. If special time-consuming tasks are required, it is recommended to use thread pool.

Different API versions of asynctask have different performance, so you need to pay attention. In order to control the length, I intend to introduce the specific usage and working principle of asynctask separately later.

The above is a few methods that Xiaobian introduced to you about how the child thread of Android development operates the UI. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time!

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
分享
二维码
< <上一篇
下一篇>>