Android Handler Update UI

Source: Internet
Author: User

Android often needs to update the value of an element in the interface, but in the main thread it is not possible to directly update the value of the main thread. It is recommended to update the values through the handler mechanism.

The definition of a handler:

Mainly accepts the data sent by the child thread, and updates the UI with this data in conjunction with the main thread.
Explanation: When the application starts, Android first opens a main thread (that is, the UI thread), the main thread is the UI control in the management interface for event distribution, for example, if you click on a button, Android will distribute the event to the button to respond to your action.  If you need a time-consuming operation at this time, for example: network read data, or read a large local file, you can not put these operations in the main thread, if you put in the main thread, the interface will appear suspended animation, if 5 seconds has not been completed, you will receive an Android system error message  Force Close. This time we need to put these time-consuming operations on a sub-thread, because the child threads involve UI updates, the Android main thread is not secure, that is, the update UI can only be updated in the main thread, and the operations in the child threads are dangerous. At this time, handler appeared to solve this complex problem, because handler runs in the mainline approached (UI thread), it and the child thread can pass the data through the Message object, at this time, the handler undertakes to accept the child thread to pass over ( The child thread uses the Sedmessage () method to transmit the message object, which contains data, which is placed in the main thread queue and updated with the main thread.

1 ProgressBar ProgressBar;2 ProgressBar progressBar2;3 4     PrivateHandler handler1;5     PrivateHandler handler=NewHandler () {6          Public voidhandlemessage (Message msg)7         {8             Switch(msg.what)9             {Ten              Case 1: One                 intCurrentprogress=progressbar.getprogress () +2; A                 if(currentprogress>Progressbar.getmax ()) -                 { -currentprogress=0; the                 } - progressbar.setprogress (currentprogress); -                  Break; -             } +         } -     }; +     Private intcount; A     PrivateRunnable DoundateProgress1 =NewRunnable () { at          - @Override -          Public voidrun () { -             //TODO auto-generated Method Stub -          -             } in      -          to         }; +     PrivateTimerTask timertask=NewTimerTask () { -          the @Override *          Public voidrun () { $             //TODO auto-generated Method StubPanax NotoginsengMessage message=NewMessage (); -message.what=1; the handler.sendmessage (message); +         } A     }; the      + @Override -     protected voidonCreate (Bundle savedinstancestate) { $ super.oncreate (savedinstancestate); $ Setcontentview (r.layout.activity_main); -Progressbar=(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR1); -Progressbar2=(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR2); theTimer timer=NewTimer (); -Timer.schedule (TimerTask,0, +);Wuyihandler1=NewHandler (); theHandler1.postdelayed (NewRunnable () { -              Wu @Override -              Public voidrun () { About                 //TODO auto-generated Method Stub $                  for(Count =0; Count <= Progressbar2.getmax (); count++) { -                      -                     //TODO auto-generated Method Stub - progressbar2.setprogress (count); A      +          the          -             } $             } the},10000); the     } the  the @Override -      Publicboolean Oncreateoptionsmenu (Menu menu) { in         //inflate the menu; This adds items to the action bar if it is present. the getmenuinflater (). Inflate (R.menu.main, menu); the         return true; About     } the  the @Override the      Publicboolean onoptionsitemselected (MenuItem item) { +         //Handle Action Bar item clicks here. The Action Bar would -         //automatically handle clicks on the Home/up button, so long the         //As you specify a the parent activity in Androidmanifest.xml.Bayi         intID =Item.getitemid (); the         if(id = =r.id.action_settings) { the             return true; -         } -         returnsuper.onoptionsitemselected (item); the}
Hanlder

In this example, the values of ProgressBar are updated by timer and handler respectively. A timer is a timer provided by Java that sets the interval of the loop execution and can be set to repeat and not repeat execution.

Android Handler Update 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.