How to use handler in Android--Update the interface in a sub-thread

Source: Internet
Author: User
Tags message queue

This article mainly introduces how to use Android handler. Handler can send messsage and runnable objects to the message queue of the thread associated with them. Each handler object is associated with the line threads that created it, and each handler object can be associated with only one line threads.

    1. Handler generally have two purposes: 1) Perform a scheduled task, you can perform certain tasks on the scheduled implementation, and you can simulate the timer. 2) Inter-thread communication. When Android apps start, a main thread is created, and the main thread creates a message queue to handle various messages. When you create a child thread, you can then get the handler object created in the parent thread from your child thread, and you can send a message to the parent thread's message queue through that object. Because Android requires the interface to be updated in the UI thread, you can update the interface in other threads with this method.

Example of updating the interface through runnable in a sub-thread

    1. 0 Create handler
    2. public class Handlertestapp extends Activity {
          &N in OnCreate bsp;   Handler Mhandler;
              TextView MText;
             /** Called when the activity is first created. */
         & nbsp;   @Override
             public void onCreate (Bundle savedinstancestate) {
                 super.oncreate (savedinstancestate);
                 Setcontentview (R.layout.main);
                 Mhandler = new Handler ();//create Handler
                  MText = (TextView) Findviewbyid (r.id.text0);//A TextView
            }

0 Build the Runnable object and update the interface in Runnable, where we have modified the TextView text. It is necessary to note that the Runnable object can be created again in the main thread or in a sub-thread. We created this here in a child thread.
Runnable MRunnable0 = new Runnable ()
{
@Override
public void Run () {
TODO auto-generated Method Stub
Mtext.settext ("This was Update from Ohter thread, Mouse down");
}
};

  1. 0 Create a sub-thread, in the thread's run function, we send a runnable to the main thread's message queue to update the interface.

    private void updateuibyrunnable () {
    New Thread ()
    {
    Message msg = Mhandler.obtainmessage ();
    public void Run ()
    {

    Mtext.settext ("This was Update from Ohter thread, Mouse down");//This sentence will throw an exception
    Mhandler.post (MRUNNABLE0);
    }
    }.start ();

    }

    Update the interface with a message in a child thread

      1. Using the message update interface is similar to the Runnable update interface, except that there are several places to modify.

    0 implement your own handler and process the message

    Private class MyHandler extends Handler
    {

            @Override
            public void Handlemessage (Message msg) {
               //TODO auto-generated Method Stub
                Super.handlemessage (msg);
                switch (msg.what)
                 {
                 Case update://updates the interface when the message is received
                     Mtext.settext ("This update by message");
                    break;
               }
           }
       }

    0 Send a message in a new thread
    private void Updatebymessage ()
        {
          & nbsp Anonymous object
             new Thread ()
              {
                    public void Run ()
                    {
                        //mtext.settext ("This was Update from Ohter thread, Mouse down");

                        //update is a self-defined integer that represents the message ID
                         Message msg = mhandler.obtainmessage (UPDATE);
                         mhandler.sendmessage (msg);
                   }
            }.start ();
       }

  2. 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.