Three solutions for updating the UI using the Handler mechanism in Android

Source: Internet
Author: User

Three solutions for updating the UI using the Handler mechanism in Android

Recently, I want to write down my Handler experience in the course of learning Android for you to view it later and share it with you.

Handler has to be connected to threads in Android or multiple threads in Java. This article will only use the most basic thread usage and won't involve too much difficulty. We will discuss about Android threads later. Every time you create an Activity in Android, this Activity (understood as the interface) is a thread, a main thread, or UI thread. The main thread can update interface elements without any problems. Every time a new Thread is created, the Thread is a sub-Thread. Android requires that the sub-Thread cannot directly update the UI. Otherwise, problems may occur.

The Handler mechanism provided by Android is used to update the UI in the Child thread. Three methods are used respectively, and the three methods are interrelated and different.

(1) Handler. post () method

This method uses the post () method in Handler to update the UI. The Thread and Runnable threads are involved here. Ignore them for the moment. This method is simple and easy to understand. First paste the Code:

 

Public class MainActivity extends Activity {private TextView text; private Handler handler = new Handler (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); text = (TextView) findViewById (R. id. id_text);/*** new Thread () implements your specific business logic in this Thread, such as network requests and time-consuming operations. * new Thread () is a subthread, non-UI Thread. If you need to update the interface in this Thread, you need to use Handler; **/new Thread () {@ Override public void run () {// In run () method to implement business logic ;//... // update the UI operation. handler. post (new Runnable () {@ Override public void run () {text. setText (updated the interface using Handler );}});}}. start ();}}

Pay attention to the comments. The developer's business logic operations on the Thread are written in the Thread. run () method, and the UI update operations are written into the Runnable. run () method. The implementation result is as follows:

 

 

(2) Handler. post (), internal class implementation Runnable interface method

This method is very similar to method 1. It only creates an internal class, implements the Runnable interface, and does not use a new anonymous internal class in the post () method. It is more logically clear. Method 1. paste the Code as follows:

 

Public class MainActivity extends Activity {private TextView text; private Handler handler = new Handler (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); text = (TextView) findViewById (R. id. id_text); final MyRunnable myRunnable = new MyRunnable (); // defines the object of MyRunnable; new Thread () {@ Override public void run () {handler. post (myRunnable); // call Handler. post method ;}}. start ();} class MyRunnable implements Runnable {// The internal class implements the Runnable interface; @ Override public void run () {// or the run () that is rewritten in Runnable () method to update the interface; text. setText (updated the interface using Handler );}}}

Note: The Thread must call the start () method, otherwise the Thread will not be executed. The implementation result is as follows:

 

 

(3) sendMessage (), handleMessage () method

There are two very important methods in Handler: sendMessage () and handleMessage (). The sendMessage () method is used to send a message to Handler in the thread. handleMessage () is used to capture the message, and update the UI. the Code is as follows:

 

Public class MainActivity extends Activity {private TextView text; private Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {switch (msg. what) {case 1: text. setText (ui updated using Handler); break ;}};@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); text = (TextView) findViewById (R. id. id_text); new Thread () {@ Override public void run (){//... your business logic; Message message = new Message (); // sends a message, which is used in handleMessage to identify who sent the Message; message. what = 1; handler. sendMessage (message );}}. start ();}}

The implementation result is the same as the above two methods:

 

.

 

Now, the UI interface has been updated using Handler in the Child thread. It can meet the most basic development needs. Of course, Handler has a lot of details and precautions. Please try again.

 

Related Article

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.