Using the handler class to update the UI

Source: Internet
Author: User

In Android, we often have to go online to get some data, and then update the UI, but it takes time to get the data, if the main thread directly using the code to get the data. The entire activity will be stuck there until you get to the end of the data update UI before the load is complete. In Android, if an activity card is 5 seconds away, it will be forcibly reclaimed.

Think back to the app we used, when it comes to loading, what's the general? Certainly will not let the interface card die there, the most incompetent also has a "load in" the circle in that turn! Then the data is loaded and disappears.

However, Android has a very subtle mechanism, the update UI can only be in the UI thread, that is, in our main thread, and access to the network only in other threads, what to do? The answer is to use the handler class.

The main idea is like this:

1, let the program start with a "load in" the Circle (Dialog).

2, handler is instantiated in the main thread, belongs to the main thread.

3, in the main thread to open another threads, used to access the Network load data, after loading, send a message to the handler class.

4, Handler received the message, update the UI, and then cancel the "load in" the circle.

Suppose a situation: to access the network, find a sentence, and then update our TextView

      PrivateHandler Handler;//Statement Hanler    PrivateTextviwe TV;//Statement TextView      protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_first_page); //instantiating handler in the main threadHandler =NewHandler () {@Override//What to do when you receive a message     Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); //Update TextView UI//cancel the "load in" boxDialog.cancel (); }};//Create a window to wait for the loadDialog =NewProgressDialog ( This);d Ialog.setmessage ("Data Initialization ...");d ialog.setcancelable (false);d Ialog.setcanceledontouchoutside (false);d ialog.show ();//open a new thread to access the network, get the dataNewThread (NewRunnable () {@Override Public voidrun () {//Access Network//send a message to handlerMessage OK =NewMessage ();    Handler.sendmessage (OK); }}). Start ();}//End OnCreate    

Briefly describe the order in which the activity is run: When you start the app, the activity will make all the statements, instantiation, layout, and so on. And then into the Dialog.show (), this place, and then the entire interface is a circle around the turn ah. At the same time, a thread is opened to access the network. Instantly a thread is spinning and turning (Dialog), another thread accesses the network, obtains data, and does not interfere with each other. When the data is finished, a message class is instantiated and the message is sent to handler. Handler after receiving the message, update the UI, then Dialog.cancel () and cancel the loop. And then what we see is the updated interface.

The above just, concise code and verbal language, should also be more understandable.

  

Using the handler class to 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.