The main thread cannot perform time-consuming operations and the child thread cannot update the UI

Source: Internet
Author: User

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
  1. Handler Mhandler = new Handler () {
  2. @Override
  3. public void Handlemessage (Message msg) {
  4. super.handlemessage (msg);
  5. switch (msg.what) {
  6. Case 0:
  7. //Finish the main interface update, get the data
  8. String data = (string) msg.obj;
  9. Updateweather ();
  10. Textview.settext (data);
  11. Break ;
  12. Default:
  13. Break ;
  14. }
  15. }
  16. };



2, the child thread sends the message, notifies handler to complete the UI update:

Java code
  1. Private void Updateweather () {
  2. New Thread (new Runnable () {
  3. @Override
  4. public Void Run () {
  5. //Time consuming operation, send message to handler after completion, complete UI update;
  6. Mhandler.sendemptymessage (0);
  7. //Need data transfer, using the following method;
  8. Message msg =new Message ();
  9. Msg.obj = "Data"; Can be the basic type, can be an object, can be a list, map, etc.;
  10. Mhandler.sendmessage (msg);
  11. }
  12. }). Start ();
  13. }


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
  1. New Thread () {
  2. public Void Run () {
  3. //Here is a time-consuming operation to update the UI after completion;
  4. Runonuithread (new Runnable () {
  5. @Override
  6. public Void Run () {
  7. //Update UI
  8. Imageview.setimagebitmap (bitmap);
  9. }
  10. });
  11. }
  12. }.start ();


If it is in a non-contextual class (Activity), it can be invoked by passing the context;

Java code
  1. Activity activity = (activity) imageview.getcontext ();
  2. Activity.runonuithread (new Runnable () {
  3. @Override
  4. public Void Run () {
  5. Imageview.setimagebitmap (bitmap);
  6. }
  7. });


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
    1. Imageview.post (new Runnable () {
    2. @Override
    3. public Void Run () {
    4. Imageview.setimagebitmap (bitmap);
    5. }
    6. });

The main thread cannot perform time-consuming operations and the child thread cannot update the UI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.