Android--handler usage: Updating the interface in a sub-thread

Source: Internet
Author: User
Tags message queue

This article mainly introduces the use of 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 a line threads that creates it. And each handler object can only be associated with a single line threads.

    1. Handler generally have two purposes: 1) Run a scheduled task, and you can run certain tasks on the scheduled implementation. Ability to simulate timers. 2) Inter-thread communication. When the Android app starts. A main thread is created, and the main thread creates a message queue to process the 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, this method allows the interface to be updated in other threads.

Example of updating an interface in a sub-thread by runnable

Create Handlerpublic class Handlertestapp extends Activity {         Handler mhandler in OnCreate;         TextView MText;         /** called when the activity is first created. *        /@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        }
Constructs a Runnable object. Update the interface in Runnable, where we have changed the text of TextView. It is necessary to note that the Runnable object can be created again in the main thread. Can also be created 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");                 }     };

creates a child 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 is Update from Ohter thread, Mouse down");//This sentence throws 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, only a few changes are needed.

To achieve their own handler. Processing 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 it receives the message                 mtext.settext ("This update by message");                 Break;}}}     Send message in new thread         private void Updatebymessage ()     {         ///Anonymous object          new thread ()          {public                 void run ()                 {                     //mtext.settext ("This was Update from Ohter thread, Mouse down");                    Update is a custom integer that represents the message ID of                     msg = Mhandler.obtainmessage (UPDATE);                     Mhandler.sendmessage (msg);                 }          }. Start ();     }



Android--handler usage: Updating the interface in a sub-thread

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.