Android does not allow the newly started thread of the activity to access the interface components in the activity. As a result, the newly started thread cannot dynamically change the attribute values of the interface components. However, in actual development, especially in the Development of animation games, the attribute values of the interface components must be changed cyclically by the newly started threads. This is achieved by the Handler message transmission mechanism.
The Handler class has two main functions:
1. Send messages in the newly started thread
2. obtain and process messages in the main thread
This process is designed with a question: when will a newly started thread send messages? When will the main thread obtain and process messages?
In order for the main thread to be "timely" to handle the messages sent by the newly started thread, it is clear that it can only be implemented through callback. What developers need to do is, override the method used to process messages in the Handler class. When a new thread sends a message, the method used to process the message in the Handler class is automatically called back.
Handler classes have many methods. Here are some common methods:
1. voidhandleMessage (Message msg); used to process messages. This method is usually used to be overwritten.
2. final boolean hasMessages (int what): Check whether the message queue contains messages with the parameter "what" as the specified value.
3. final boolean hasMessages (int what, Object object): Check whether the message queue contains messages with the parameter "what" as the specified value and the parameter object as the specified Object.
4. final Message obtainMessage (): gets a Message from the Message pool.
5. final boolean sendEmptyMessage (int what): Send an empty message
6. final boolean sendEmptyMessageDelayed (int what, long delayMillis): specify how many milliseconds to send an empty message
7. final boolean sendMessage (Message msg): send the Message immediately
8. final boolean sendMessageDelayed (Message msg, long delayMillis): specify the number of milliseconds to send messages.
Through the above methods, the program can easily implement message transmission!
Paste the Code:
[Java]
<SPAN style = "FONT-SIZE: 14px"> package com. test. handler;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Private int MESSAGE = 1;
Private TextView mTextView;
Private String temp = "the thread processes every two seconds ";
Private Thread mThread;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MTextView = (TextView) findViewById (R. id. handTextView );
// Only implement Thread
Thread. start (); // start the thread
//// Runnable implementation
// MThread = new Thread (mRunnable );
// MThread. start ();
// This is the most concise method.
// MTextView. post (new Runnable (){
//
// @ Override
// Public void run (){
// Message message = new Message ();
// Message. what = MESSAGE;
// MHandler. sendMessage (message );
//}
//});
}
// The handleMessage method of Handler processes and updates the ui.
Handler mHandler = new Handler (){
@ Override
Public void handleMessage (Message msg) {// This method runs in the main thread
Super. handleMessage (msg );
Switch (msg. what ){
Case 1:
MTextView. setText (temp );
Break;
Default:
Break;
}
}
};
// Thread implementation
Thread thread = new Thread (){
@ Override
Public void run () {// newly created thread
Super. run ();
For (int I = 0; I <100; I ++ ){
Try {
Sleep (2000); // pause for 2 seconds before sending messages
Temp = temp + I;
// The first method for sending messages
// Message message = new Message ();
// Message. what = MESSAGE;
// MHandler. sendMessage (message );
// The second method for sending messages. We recommend that you use this method.
Message message = mHandler. obtainMessage ();
MHandler. obtainMessage (MESSAGE). sendToTarget ();
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
};
// Runnable implementation
Runnable mRunnable = new Runnable (){
@ Override
Public void run (){
For (int I = 0; I <100; I ++ ){
Try {
Thread. sleep (2000); // pause for 2 seconds before sending messages
Temp = temp + I;
// The first method for sending messages
// Message message = new Message ();
// Message. what = MESSAGE;
// MHandler. sendMessage (message );
// The second method for sending messages. We recommend that you use this method.
Message message = mHandler. obtainMessage ();
MHandler. obtainMessage (MESSAGE). sendToTarget ();
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
};
}
</SPAN>