Android interface refresh

Source: Internet
Author: User

Both Android invalidate and postinvalidate are used to refresh the interface. The usage difference is as follows:

1) invalidate ():Instantiate a handler object and rewrite the handlemessage method to call invalidate () to refresh the interface. The online program sends the interface to update the message through sendmessage.

 //  Enable thread in oncreate () 
New Thread ( New Gamethread (). Start ();

// Instantiate a handler
Handler myhandler = New Handler (){
// Post-processing of received messages
Public Void Handlemessage (Message MSG ){
Switch (Msg. What ){
Case Activity01.refresh:
Mgameview. Invalidate () ; // Refresh page
Break ;
}

Super . Handlemessage (MSG );
}
};

Class Gamethread Implements Runnable {
Public Void Run (){
While (! Thread. currentthread (). isinterrupted ()){
Message message = New Message ();
Message. What = activity01.refresh;
// Send message
Activity01.This . Myhandler. sendmessage (Message );
Try {
Thread. Sleep (100 );
} Catch (Interruptedexception e ){
Thread. currentthread (). Interrupt ();
}
}
}
}

2)Using postinvalidate is relatively simple and requires no handler. You can directly call postinvalidate in the thread.

ClassGamethreadImplementsRunnable {
Public VoidRun (){
While(! Thread. currentthread (). isinterrupted ()){
Try{
Thread. Sleep (100 );
}Catch(Interruptedexception e ){
Thread. currentthread (). Interrupt ();
}

//You can use postinvalidate to directly update the interface in the thread.
Mgameview. postinvalidate ();
}
}
}

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.