Android provides a long wait Interface

Source: Internet
Author: User

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.

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.