Android-timer and handler

Source: Internet
Author: User

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 );
}

 

 

 

 

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.