There are often problems with Android projects that update the UI after a time-consuming operation in a child thread, and then summarize the updated methods for some of the projects that you have experienced:
Look at the message mechanism in Android before looking at the method:
Reference
Message: Messages that contain the message ID, the message Processing object, and the processed data are queued by MessageQueue and processed by handler.
Handler: Handler, responsible for sending and processing the message. When using handler, you need to implement the Handlemessage (Message msg) method to process a specific message, such as updating the UI.
MessageQueue: Message Queuing, which holds messages sent by handler and executes according to FIFO rules. Of course, storing a message is not a meaningful preservation, but rather a concatenation of the message in the form of a linked list, waiting for the looper to be extracted.
Looper: The message pump continuously extracts message execution from the MessageQueue. Therefore, a MessageQueue needs a looper.
Thread: Threads that are responsible for dispatching the entire message loop, that is, the execution site of the message loop.
Unfamiliar friends can refer to this document:
Android Knowledge Grooming: The handler:http://gqdy365.iteye.com/blog/2148925 of message mechanism
Android Knowledge Grooming: The looper:http://gqdy365.iteye.com/blog/2137494 of message mechanism
The following principle is based on the above-mentioned Update method:
Method One: With handler
1, the main thread defined handler:
Java code
- Handler Mhandler = new Handler () {
- @Override
- public void Handlemessage (Message msg) {
- super.handlemessage (msg);
- switch (msg.what) {
- Case 0:
- //Finish the main interface update, get the data
- String data = (string) msg.obj;
- Updateweather ();
- Textview.settext (data);
- Break ;
- Default:
- Break ;
- }
- }
- };
2, the child thread sends the message, notifies handler to complete the UI update:
Java code
- Private void Updateweather () {
- New Thread (new Runnable () {
- @Override
- public Void Run () {
- //Time consuming operation, send message to handler after completion, complete UI update;
- Mhandler.sendemptymessage (0);
- //Need data transfer, using the following method;
- Message msg =new Message ();
- Msg.obj = "Data"; Can be the basic type, can be an object, can be a list, map, etc.;
- Mhandler.sendmessage (msg);
- }
- }). Start ();
- }
The handler object of method one must be defined in the main thread, if multiple classes are called directly to each other, it is not very convenient to pass the Content object or call through the interface;
Method Two: Update with the Runonuithread method of activity object
Update the UI with the Runonuithread () method in a child thread:
Java code
- New Thread () {
- public Void Run () {
- //Here is a time-consuming operation to update the UI after completion;
- Runonuithread (new Runnable () {
- @Override
- public Void Run () {
- //Update UI
- Imageview.setimagebitmap (bitmap);
- }
- });
- }
- }.start ();
If it is in a non-contextual class (Activity), it can be invoked by passing the context;
Java code
- Activity activity = (activity) imageview.getcontext ();
- Activity.runonuithread (new Runnable () {
- @Override
- public Void Run () {
- Imageview.setimagebitmap (bitmap);
- }
- });
This method is more flexible to use, but if the thread is defined elsewhere, the activity object needs to be passed;
Method Three: View.post (Runnable R)
Java code
- Imageview.post (new Runnable () {
- @Override
- public Void Run () {
- Imageview.setimagebitmap (bitmap);
- }
- });
The main thread cannot perform time-consuming operations and the child thread cannot update the UI