Several ways Android updates the UI

Source: Internet
Author: User

1, activity of the Runonuithread
TextView = (TextView) Findviewbyid (r.id.tv);        New Thread (New Runnable () {            @Override public            void Run () {                runonuithread (new Runnable () {                    @Override Public                    void Run () {                        Textview.settext ("Updated UI");}})        . Start ();
The android Activity Runonuithread () method uses2, Handler sendemptymessage ()
Package Lib.com.myapplication;import Android.os.handler;import Android.os.message;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.widget.textview;public Class    Mainactivity extends Appcompatactivity {private TextView TextView; Handler Handler = new Handler () {@Override public void Handlemessage (Message msg) {Super.handl            EMessage (msg);        Textview.settext ("UI updated");    }    };        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        TextView = (TextView) Findviewbyid (r.id.tv); New Thread (New Runnable () {@Override public void run () {try {Th                Read.sleep (2000);                } catch (Interruptedexception e) {e.printstacktrace ();            } handler.sendemptymessage (2);       } }). Start (); }}
3, Handler  post ()
Package Lib.com.myapplication;import Android.os.bundle;import Android.os.handler;import Android.support.v7.app.appcompatactivity;import Android.widget.textview;public class MainActivity extends    appcompatactivity {private TextView TextView;    Handler Handler = new Handler ();        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        TextView = (TextView) Findviewbyid (r.id.tv); New Thread (New Runnable () {@Override public void run () {try {Th                Read.sleep (2000);                } catch (Interruptedexception e) {e.printstacktrace ();                        } handler.post (New Runnable () {@Override public void run () {                    Textview.settext ("UI updated");            }                }) ;    }}). Start (); }}
4. View Post ()
TextView = (TextView) Findviewbyid (r.id.tv);        New Thread (New Runnable () {            @Override public            void Run () {                try {                    thread.sleep ()                } catch (Interr Uptedexception e) {                    e.printstacktrace ();                }                Textview.post (New Runnable () {                    @Override public                    void Run () {                        textview.settext ("UI updated");}                ) ;            }        }). Start ();

Summarize:

1, in fact, the above four ways can be attributed to one way: handler used for communication between Android threads.

2. Why does Android require UI manipulation only on the UI thread? The main thing is to avoid the problem of concurrency caused by multithreading. The UI is secure in a single-threaded operation.

Several ways Android updates 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.