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:



 Timer timer = new timer ();
Handler handler = new handler (){
@ Override
Public void handlemessage (Message MSG ){

Switch (msg. What ){
Case 1:
Settitle ("actually changed ");
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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.