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

Source: Internet
Author: User

Recently want to learn the Android process in the handler experience to write down, for yourself to see later, also share with you.

Using handler actually has to do with threads in Android or multithreading in Java. This article only uses the most basic thread to use, does not involve too the Android thread we will discuss later. For each new activity in Android, the activity (understood as the interface) is a thread, which is also called the UI thread. The main thread can update the interface elements without any problems. Whenever a new thread is created, the thread is a child thread, and Android specifies that the child thread cannot update the UI interface directly, otherwise it will be problematic.

The following will use the handler mechanism provided by Android to update the UI in a child thread. There are 3 ways to use each other and 3 ways to connect and differentiate.

(1) handler.post () mode

This method updates the UI using the Post () method in handler, which involves the concept of a thread,runnable two thread, which is ignored. This method is relatively simple and easy to understand. First put 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, time-consuming operations, and so on;         * New Thread () is a child thread, 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 the Run () method to implement business logic;                //... Update UI action;                handler.post (New Runnable () {                    @Override public                    void Run () {                        Text.settext (" Use handler to update the interface ");}}                );}        . Start ();    }}

Notice the comments. The developer's business logic operation on the thread is written in the Thread.run () method, and the update UI is written to the Runnable.run () method. The implementation results are as follows:


(2) Handler.post (), inner class implements runnable interface mode

This method is very similar to Method 1, just creates an inner class, implements the Runnable interface, and then does not use the new anonymous inner class in the post () method. relatively logically clearer. Ideas with 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 ();//define Myrunnable object;        new Thread () {            @Override public            void Run () {                handler.post (myrunnable);//Call handler.post method;            }        }.start ();    }    Class Myrunnable implements Runnable {//inner class implements Runnable interface;        @Override public        void Run () {//or Runnable-rewritten run () Method update interface;            Text.settext ("Updated interface with Handler");}}        }    

Note that the thread must call the start () method, otherwise threads will not be executed. The implementation results are as follows:


(3) SendMessage (), handlemessage () mode

There are two very important methods in handler, the SendMessage () and Handlemessage () methods, the SendMessage () method is used to send a message to handler in a thread, and 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 ("Updated interface with 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 ();//Send a message that is used to distinguish who sent the message in handlemessage;                message.what = 1;                Handler.sendmessage (message);            }        }. Start ();    }}

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


At this point, it has been basically implemented to update the UI interface with handler in a child thread. To meet the most basic development needs. Of course, there are many details and precautions in handler, please listen to tell.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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

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.