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 ();
}
}
}