Timer is a class for executing tasks. It accepts timertask parameters;
There are two ways for timer to execute a task. One is to execute a task timer at a specified time. schedule (timertask task, data), the other is how long it takes to execute a task timer. schedule (timertask task, long delay );
Timertask executes the specified task. However, because Android is thread-safe, all interface updates must be placed in the main thread, so handler objects must be used.
The subject code is as follows:
Code
Timer timer = new timer ();
Handler handler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
Switch (msg. What ){
Case 1:
Settitle ("actually changed ");
Break;
}
Super. handlemessage (MSG );
}
};
Timertask task = new timertask (){
@ Override
Public void run (){
// Todo auto-generated method stub
// Because the main thread is secure, page updates must be stored in the main thread
Message MSG = new message ();
MSG. What = 1;
Handler. sendmessage (MSG );
}
};
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Change the title one second later
Timer. Schedule (tasks, 1000 );
}