There are often problems in Android projects. After completing time-consuming operations in child threads, you need to update the UI. Here are some projects you have experienced to summarize the update method:
Before looking at the method, you need to understand the message mechanism in Android.
Method 1
The method is as follows:
runOnUiThread(new Runnable() { @Override public void run() { ("Hello"); } });
This method is simple and easy to use. If the current thread is a UI thread, the action is to execute 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 Handler. It adds this request message to update the UI to the event queue and waits for execution when the main thread is idle.
Method 2 Handler
The Handler is defined in the main thread as follows:
Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { (msg); switch () { case 0: String data = (String); (data); break; default: break; } } };
The child thread sends a message to notify the Handler to complete the UI update, and the code is as follows:
new Thread(new Runnable(){ @Override public void run() { //Time-consuming operation(0); Message msg =new Message(); = "data";//It can be a basic type, it can be an object, it can be a List, map, etc.(msg); } }).start();
Method 3
final Button btn =(Button)findViewById(); (new Runnable(){ @Override publicvoid run() { ("Hello"); } });
The above code is to update the content in btn, and the following code can also achieve this effect.
Handler handler = new Handler(); final Button btn = (Button)findViewById(); (new Runnable(){ @Override public void run() { ("Hello"); } });
This is a method, and one is a method. The method has been introduced in Android's message mechanism. In fact, the send method in method 2 was called in the end.
Now let’s look at the source code of the method:
public boolean post(Runnable action) { Handler handler; AttachInfo attachInfo = mAttachInfo; if (attachInfo != null) { handler = ; } else { // Assume that post will succeed later ().post(action); return true; } return (action); }
The main functional code in the method is to obtain the Hanlder of the current thread (i.e., the UI thread), and then post the action object into the Handler. The link text above the processing process in Handler has been analyzed very clearly. It wraps the passed action object into a Message (Message's callback is action), and then throws it into the message loop of the UI thread. In Handler's dispatchMessage method, the first sentence is to set it and directly call the runnable run method. At this time, it has been routed to the UI thread, so we can update the UI without any worries.
Method 4 Broadcast
A broadcast is sent in the child thread, and a broadcast is received in the main thread and the UI is updated.
Method 5 Using AsyncTask
In order to simplify accessing the UI in child threads, the system provides us with AsyncTask.
AsyncTask is a lightweight asynchronous task class that can execute background tasks in a 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 particularly time-consuming background tasks. If you need to perform particularly time-consuming tasks, it is recommended to use thread pools.
Different API versions of AsyncTask have different performances, so you need to pay attention to it. In order to control the length, the specific usage and working principles of AsyncTask, I plan to introduce it in a separate article later.
The above are several methods of how the Android development son thread operates the UI introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!