The Android implementation waits for the interface. Generally, when we perform time-consuming operations, the interface thread cannot be blocked. Otherwise, if android finds that the application is blocked for too long, a dialog box is displayed to close the application, therefore, we generally place time-consuming operations on another thread for execution, and then notify the interface thread to complete the corresponding operations after the operations are completed.
Message can return different types of values to meet different requirements.
// Declare Variables
Private Button b1;
Private ProgressDialog pd;
// Define the Handler object
Private Handler handler = new Handler (){
@ Override
// Execute the Handler method when a message is sent.
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
// Close the dialog box as long as it is executed.
Pd. dismiss ();
}
};
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
Private void processThread (){
// Build a download progress bar
Pd = ProgressDialog. show (MainHandler. this, "Synchronize", "synchronizing... ");
New Thread (){
Public void run (){
// The method used to consume the execution time
LongTimeMethod ();
// Send a message to handler after execution
Handler. sendEmptyMessage (0 );
}
}. Start ();
}
This method is concise and highly efficient.