Android thread controls UI Update (Handler, post (), postDelayed (), postAtTime)

Source: Internet
Author: User

Android thread controls UI Update (Handler, post (), postDelayed (), postAtTime)

According to the following understanding, handler has a certain relationship with the ui thread, because the update interface can only be in all the update interfaces in the main thread, and the update interface can be in the handleMessage that receives messages. port (new Runnable) in the Custom thread and then execute the post method to notify the main thread to update this interface

The following is a reference:

Android threads are single-threaded. Therefore, Handler is used to update the UI:

Private Handler splashHandler = new Handler (){
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case OVER:
ProgressDialog. dismiss ();
Break;
Default:
Break;
}
Super. handleMessage (msg );
}
}. After a thread is running, a notification can be sent to Hanler to notify 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); Update the UI directly in Runnable, because Runnable is running in the main thread of the UI.

Android thread controls UI Update (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.