Detailed description of timer in Android

Source: Internet
Author: User
Tags repetition

Go directly to the code and explain the comments. An example of a rocket launch countdown

Main. xml

[HTML] <? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
 
<Button
Android: Id = "@ + ID/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Start countdown"/>
 
<Textview
Android: Id = "@ + ID/textview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
 
</Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">

<Button
Android: Id = "@ + ID/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Start countdown"/>

<Textview
Android: Id = "@ + ID/textview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

</Linearlayout>
Timerdemoactivity. Java

[Java] package com. tianjf;
 
Import java. util. timer;
Import java. util. timertask;
 
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. textview;
 
Public class timerdemoactivity extends activity implements onclicklistener {
 
Private button;
Private textview;
Private timer;
 
// Define Handler
Handler handler = new handler (){
 
@ Override
Public void handlemessage (Message MSG ){
Super. handlemessage (MSG );
 
Log. D ("debug", "thread where the handlemessage method is located :"
+ Thread. currentthread (). getname ());
 
// Handler processes the message
If (msg. What> 0 ){
Textview. settext (msg. What + "");
} Else {
Textview. settext ("ignition! ");
// End the timer Timer
Timer. Cancel ();
}
}
};
 
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
 
Button = (button) findviewbyid (R. Id. Button );
Textview = (textview) findviewbyid (R. Id. textview );
 
Log. D ("debug", "thread where the oncreate method is located :"
+ Thread. currentthread (). getname ());
Button. setonclicklistener (this );
}
 
@ Override
Public void onclick (view v ){
Switch (V. GETID ()){
Case R. Id. Button:
// Create a timer when the button is pressed
Timer = new timer ();
// Create a timertask
// Timertask is an abstract class that implements the runnable interface. Therefore, timertask is a subthread.
Timertask = new timertask (){
// Countdown 10 seconds
Int I = 10;
 
@ Override
Public void run (){
Log. D ("debug", "thread where the run method is located :"
+ Thread. currentthread (). getname ());
// Define a message to pass
Message MSG = new message ();
MSG. What = I --;
Handler. sendmessage (MSG );
}
};
// Define a scheduled task and perform the following tasks based on different parameters:
// 1. Schedule (timertask task, date when) Schedule> execute a task at a fixed time
// 2. Schedule (timertask task, date when, long period) Progress> repeat a task at a fixed time, with controllable repetition Interval
// 3. Schedule (timertask task, long delay) Delay> after how long the task will be executed
// 4. Schedule (timertask task, long delay, long period) interval> repeat a task after the delay, with controllable repetition Interval
Timer. Schedule (timertask, 3000,100 0); // The countdown starts after 3 seconds. The countdown interval is 1 second.
Break;
 
Default:
Break;
}
}
}

 

From the column of zookeeper

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.