Handler handles long-time events

Source: Internet
Author: User

Handler is required when we are dealing with some long-time events, such as reading network or database data, sometimes, in order not to affect the smoothness of user operations on applications, we need to open one more thread to differentiate UI threads and process long-time operations in new threads. Handler can be used for data processing during development, and various effects can be implemented after flexible modification.
Step 1: The ssdialog dialog box is displayed, prompting you to perform operations for a long time.
Step 2: open one more thread to process long-time operations. Here, let the thread sleep for 10 seconds.
Part 3: Send a message to handler after a long operation, which can be an integer. If the Activity has multiple handler, you can define multiple integer member variables to distinguish different messages.
Step 4: Drop the ProgressDialog object dismiss () in the handlerMessage method that defines Handler, and delete the pop-up box to indicate that the operation is complete.
The Code is as follows:
[Java]
Public class Android_HandlerActivity extends Activity {
/** Called when the activity is first created .*/
// Declare Variables
Private Button b1;
Private ProgressDialog pd;
// Define the handler object
Private Handler handler = new Handler (){
// Execute the Handler method when a message is sent.
@ Override
Public void handleMessage (Message msg ){
// TODO Auto-generated method stub
Super. handleMessage (msg );
// Close the dialog box as long as it is executed.
Pd. dismiss ();
}

};
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

// Resources res = this. getResources ();

// View the thread name of the UI component
Log. I ("tag", "onCreate () -->" + Thread. currentThread (). getName ());
 
 
// Define the UI component
B1 = (Button) findViewById (R. id. Button01 );

// Bind the button to the Click Event listener
B1.setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Click the button to process long-time operations
ProcessThread ();
}
});
}
Protected void processThread (){
// TODO Auto-generated method stub
// Build a download progress bar
Pd = ProgressDialog. show (Android_HandlerActivity.this, "download file", "download ...... ");
Log. I ("tag", "processThread () -->" + Thread. currentThread (). getName ());
New Thread (){
 
 
@ Override
Public void run (){
Log. I ("tag", "run () -->" + Thread. currentThread (). getName ());
 
 
// Method of CEO time consumption in the new thread
LongTimeMethod ();
 
 
// Send an empty message to handler after execution.
Handler. sendEmptyMessage (0 );
}

}. Start ();
}
// Simulate the time-consuming Method for downloading objects
Protected void longTimeMethod (){
// TODO Auto-generated method stub
Try {
 
 
Log. I ("tag", "longTimeMethod -->" + Thread. currentThread (). getName ());
 
 
Thread. sleep (10000 );
 
 
} Catch (InterruptedException e ){
 
 
E. printStackTrace ();
 
 
}
}
}
Main. xml is just a simple Button:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<Button
Android: id = "@ + id/Button01"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
 
</LinearLayout>
The running image is as follows:








From the column of heirenheiren

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.