Android timer chronometer

Source: Internet
Author: User

:

Activity Code:

Package cn.com. chenzheng_java; </P> <p> Import Java. util. timer; <br/> Import Java. util. timertask; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. OS. systemclock; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. chronometer; <br/>/** <br/> * @ description chronom Eter timer application instance <br/> * @ author chenzheng_java <br/> * @ since 2011/03/17 <br/> */<br/> public class chronometeractivity extends activity {<br/> chronometer; </P> <p> timer = new timer (); <br/> timertask task = new timertask () {</P> <p> @ override <br/> Public void run () {<br/> chronometer. stop (); <br/> log. I ("notification", "Timing ends"); <br/>}< br/> }; </P> <p> @ override <br/> protected void oncreate (B Undle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. chronometer); </P> <p> chronometer = (chronometer) findviewbyid (R. id. chronometer1); <br/> button = (button) findviewbyid (R. id. button_ch); </P> <p> button. setonclicklistener (new view. onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/>/** <br/> * systemclock. elapsedrealtime () is obtained from startup The start time. After it is set, the timer starts timing from. <br/> * systemclock. currentthreadtimemillis () gets the running time of the current thread <br/> * systemclock. setcurrenttimemillis (long millis) sets the current time <br/> * chronometer. the setbase () method can be used to set the start time of the Current timer <br/> */<br/> chronometer. setbase (systemclock. elapsedrealtime (); <br/>/** <br/> * set the actual format. You can specify a string containing % S, then chronometer <br/> * will automatically place the MM: SS time at your first % s location. <Br/> */<br/> chronometer. setformat ("Current Time: % s"); <br/> // start <br/> chronometer. start (); <br/> log. I ("notification", "Start Time"); <br/> handler = new handler (); <br/> handler. post (New runnable () {</P> <p> @ override <br/> Public void run () {<br/> timer. schedule (task, 10000); <br/>}< br/>}); <br/>}< br/> }); </P> <p >}< br/>

Chronometer. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "match_parent" Android: layout_height = "match_parent" <br/> Android: orientation = "vertical"> <br/> <chronometer Android: Id = "@ + ID/chronometer1" Android: text = "chronometer" Android: gravity = "center_horizontal" <br/> Android: layout_width = "match_parent" Android: layout_height = "wrap_content"> </chronometer> <br/> <button Android: TEXT = "click" Android: Id = "@ + ID/button_ch" <br/> Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <br/> </linearlayout> <br/>

----------------------------------------------------------------------

Chronometer is a timer which we can set. The main methods are:

We can also see that we can register a chronometer. onchronometerticklistener event. This event will be triggered whenever the chronometer time changes, that is, it will be triggered once every second.

 

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.