Address: http://www.eoeandroid.com/thread-72298-1-1.html
Handler basic concepts:
Handler is mainly used for Asynchronous Message Processing: when a message is sent, it first enters a message queue and the function that sends the message returns immediately. The other part extracts the message one by one in the message queue, then the message is sent out, that is, sending and receiving messages are not processed synchronously. This mechanism is usually used to process operations that are relatively time-consuming.
A simple example is used to introduce Handler.
Example 1: An application has two buttons (start and end). When the start button is clicked, a thread is executed. This thread outputs a string on the console, the thread is executed again every three seconds until the end button is clicked.
For the application interface:
For the console output when the program is executed:
Development steps:
1. Create an Android Application
2. Add two Button control labels to the layout file and set the attributes and values for them.
3. Declare the control variable in the Activity and obtain the control object based on the id.
4. Create a Handler object in Activity
5. Create a Runnable object in the Activity.
A) using anonymous internal classes
B) write the operation to be executed in the run () method of the Runnable object.
I. Print a sentence
Ii. Call the postDelayed () method of the Runnable object
6. Compile the listener required by the start button in the Activity and bind it
A) In The Onclick () method of the listener, call the post () method of Handler and place the thread object to be executed in the queue.
7. Compile the listener required by the end button in the Activity and help determine
A) In The Onclick () method of the listener, call the removeCallbacks () method of Handler to delete unexecuted thread objects in the queue.
B)
The following is the Activity code:
Package eoe. demo;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class HandlerTest extends Activity {
/** Called when the activity is first created .*/
Private Button startButton;
Private Button endButton;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Obtain the control object by id
StartButton = (Button) findViewById (R. id. startButton );
EndButton = (Button) findViewById (R. id. endButton );
// Set the listener for the control
StartButton. setOnClickListener (new StartButtonListener ());
EndButton. setOnClickListener (new EndButtonListener ());
}
Class StartButtonListener implements OnClickListener {
Public void onClick (View v ){
// Call the post () method of Handler to put the thread object to be executed in the queue
Handler. post (updateThread );
}
}
Class EndButtonListener implements OnClickListener {
Public void onClick (View v ){
// Call the removeCallbacks () method of Handler to delete unexecuted thread objects in the queue
Handler. removeCallbacks (updateThread );
}
}
// Create a Handler object
Handler handler = new Handler ();
// Create a thread object
Runnable updateThread = new Runnable (){
// Write the operation to be executed in the run method of the thread object
Public void run (){
System. out. println ("updateThread ");
// Call the postDelayed () method of Handler
// The function of this method is to put the thread object to be executed into the queue and run the specified thread object after the end of time.
// The first parameter is of the Runnable type: the thread object to be executed.
// The second parameter is of the long TYPE: Delay Time, in milliseconds
Handler. postDelayed (updateThread, 3000 );
}
};
}
The above is the simplest example. Let's look at another example.
Example 2: An application has a progress bar and a button. When a button is clicked, the progress bar advances every second.
Run the application:
Development steps:
1. Create an Android Application
2. Add a progressBar and a Button in the layout file, and set attributes and values for it.
3. Declare the control variable in the Activity and obtain the control object based on the id.
4. Create a thread object
A) using anonymous internal classes
B) write the operations in the thread object after completing step 5 and Step 6.
I. Declare a variable to set the progress of the progress bar.
Ii. Override the run method () of the thread class to write the operations to be executed.
1. Print a string
2. The value of the progress bar is increased.
3. Get a message object
4. Set the arg1 value of the message object.
5. Sleep the thread for one second
6. Put the message object into the Message Queue
7. If the progress bar value is 100, the thread object will be removed from the queue.
5. Create a Handler object
A) The difference from Example 1 is that it is declared through an anonymous internal class, and Example 1 is an object directly new.
B) override the handlerMessage (Message msg) method of the Handler object
I. This method passes in a Message object, that is, the Message object. First, set the progress of the progress bar (this value is a member variable arg1 IN THE mescript object ).
Ii. Put the thread object to be executed into the queue
6. Compile the listener required by the Button and bind the listener.
A) set the progress bar to display status
B) Put the thread object to be executed into the queue
Package eoe. demo;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ProgressBar;
Public class ProgressBarHandlerTest extends Activity {
/** Called when the activity is first created .*/
Private ProgressBar progressBar;
Private Button startButton;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ProgressBar = (ProgressBar) findViewById (R. id. progressbar );
StartButton = (Button) findViewById (R. id. startButton );
StartButton. setOnClickListener (new ProgressBarOnClickListener ());
}
Class ProgressBarOnClickListener implements OnClickListener {
Public void onClick (View v ){
// Set the progress bar to visible
ProgressBar. setVisibility (View. VISIBLE );
UpdateBarHandler. post (updateThread );
}
}
// Use an anonymous internal class to rewrite the handlerMessage () method in the Handler.
Handler updateBarHandler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
ProgressBar. setProgress (msg. arg1 );
UpdateBarHandler. post (updateThread); // put the thread to be executed into the queue
}
};
// Thread class, which is declared using an anonymous internal class
Runnable updateThread = new Runnable (){
Int I = 0;
Public void run (){
// TODO Auto-generated method stub
System. out. println ("Begin Thread ");
I + = 10;
// Get a Message object. The Message class is provided by the android system.
Message msg = updateBarHandler. obtainMessage ();
// Set the arg1 parameter value of the Message object to I
Msg. arg1 = I; // use the two member variables arg1 and arg2 to transmit messages. The advantage is that the system performance consumption is low.
Try {
Thread. sleep (1000); // sleep the current Thread for 1000 milliseconds
} Catch (InterruptedException ex ){
Ex. printStackTrace ();
}
// Add the Message object to the Message Queue
UpdateBarHandler. sendMessage (msg );
// If the I value is 100
If (I = 100 ){
// Remove the thread object from the queue
UpdateBarHandler. removeCallbacks (updateThread );
}
}
};
}