Objective
Believe that beginners Android development friends, should encounter a problem, we opened a thread, in this thread we have to update the UI operation, perhaps in the TextView display a line of text, perhaps changed the ImageView display of the picture, Although it just seems simple and correct operation, but the Android system let your program glorious collapse, and you do not know why wrong, this is the most painful, has been deeply affected by this pain, in order not to let this pain spread, I decided to update the UI a few ways to give everyone a good say, Let everyone in the thread of the Run method to update the UI as you wish, no more pain.
Realize
Using the Post method of the handler class
We first need to generate an object of the handler class in mainactivity without implementing the Handmessage method, because the object of the handler class is not processing the message at this time but acting as the role sending the message.
handler.post(new Runnable() { @Override publicvoidrun() { mTextView.setText("OK"); } });
In this way, we go in and out of a runnable, in which we implement the code that updates the UI, and then we can update it without crashing.
- Using Handler's own method of handling messages
We know that handler can send messages or process messages, the first way we use the ability to send messages, and now we use it to handle the functionality of messages. We need to build a handler object, and we need to overwrite its method of handling the message, and then we'll implement what we need, as shown here:
privatenew Handler(){ publicvoidhandleMessage(android.os.Message msg){ mTextView.setText("ok"); }; };
As you can see, I covered the Handlemessage method, and in it I added the code to update the UI, and of course it was correct to update the UI successfully. Then we need to call handler in the Mainactivity method of sending messages, to send themselves a message, and then to process, we can simply call a method to send an empty message can: Handler.sendemptymessage (0);
- Updating the UI in the Runonuithread method
runOnUiThread(new Runnable() { @Override publicvoidrun() { mTextView.setText("updateUI->ok"); } });
Runonuithread method, according to its name we can know that this method may be used to update the UI, because the update UI must be in the UI thread, other threads are not allowed to update the UI, so we passed a runnable in the Runonuithread method, Then we can do it in the inside. This method and the first method is still very similar, I personally still prefer this method, because it does not need a handler object, I feel very convenient.
- The view calls the Post method to implement the update UI
The other three methods we are all through other classes and methods to achieve the update of the view, the last method is to view their own methods to implement the update, but it is similar to the problem, the final implementation of the principle is similar.
mTextView.post(new Runnable() { @Override publicvoidrun() { mTextView.setText("ViewUI->ok"); } });
We also need to pass in the runnable, and then update it.
Summarize
Because the Android system is designed to not let us in the non-UI thread to update the UI, not only we can not connect the UI thread to the operation of the network, if you do not know the friend may be dead (I have been in the pit), all for the security of the UI thread, If we need to do an update on the UI thread, such as displaying the download progress, updating the progress bar, and so on, we need an object that can communicate between the UI thread and the non-UI thread, informing the UI thread to update the non-UI thread's requirements, which I think handler should be. As for the importance of handler, I am not tired of the said, interested people can find some of the relevant information on their own to understand the existence of handler mechanism and the use of methods, I believe that the development is very helpful.
Four ways Android updates the UI