Android Threading Controls UI updates (Handler, post (), postdelayed (), Postattime)

Source: Internet
Author: User

The following understanding is that handler and the UI thread have a certain association can be because the update interface can only be in the main thread all the update interface in the handlemessage of receiving the message there is also the update interface can be Handler.port (new Runnable) In a custom thread, then execute the Post method to notify the main thread to update the interface

Here are the references:

The Android thread is single-threaded. So update the UI to use handler:

private Handler splashhandler = new Handler () {
     public void Handlemessage ( Message msg) {
       switch (msg.what) {
       case-over:
        Progressdialog.dismiss ();
        break;
       Default:
            Break
      }
          super.handlemessage (msg);
    }
}; When a thread finishes running, it can send a notification to Hanler, notifying the main thread to update the UI:

New Thread () {public void run () {
Initlist ();
Message m = splashhandler.obtainmessage (over);
Splashhandler.sendmessage (m);
Splashhandler.post (R);
Splashhandler.postdelayed (R, Delaymillis);
Splashhandler.postattime (R, Uptimemillis);

}}.start (); can also be used directly:

Splashhandler.post (New Runnable () {
public void Run () {
Progressdialog.dismiss ();
}
});
Splashhandler.postdelayed (R, Delaymillis);
Splashhandler.postattime (R, Uptimemillis); Updates the UI directly in Runnable because the Runnable at this point is running in the main thread where the UI is located.

Android Threading Controls UI updates (Handler, post (), postdelayed (), Postattime)

Android Threading Controls UI updates (Handler, post (), postdelayed (), Postattime)

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.