Timer: chronometer

Source: Internet
Author: User

The android SDK provides multiple time services. These time services mainly process tasks that occur at a certain interval or a certain time in the future. In the Android system, the time service scope can be either the application itself or the entire android system. This section describes how to use these time services and provides a large number of instances for readers to learn.

Timer: chronometer

The project directory of the sample code in this section is src/ch08/ch08_chronometer.

Chronometer is a subclass of textview and an android component. This component can time with a 1 second interval and display the timing result.

The chronometer class has three important methods: start, stop, and setbase. The start method indicates start timing, the stop method indicates stop timing, and the setbase method indicates re-timing. The Start and Stop methods do not have any parameters. The setbase method has a parameter that indicates the benchmark time for starting timing. If you want to re-timing from the current time point, you can set this parameter value to systemclock. elapsedrealtime ().

You can also set the chronometer component. By default, the chronometer component outputs only the time format of MM: SS or H: mm: Ss. For example, when the timer reaches 1 minute 20 seconds, the chronometer component displays. To change the displayed information, use the setformat method of the chronometer class. This method requires a string variable and uses "% s" to indicate timing information. For example, if you use setformat ("timing information: % s") to set the display information, the chronometer component displays the following timing information:

Timing information: 10: 20

The chronometer component can also capture timing actions through the onchronometertick event method. This method is called once per second. To use the onchronometertick event method, you must implement the following interface:

 
 
  1. android.widget.Chronometer.OnChronometerTickListener 

In this example, three buttons are used to start, stop, and reset the timer, and the current time is displayed through the onchronometertick event method. The Code is as follows:

 
 
  1. Package net. blogjava. Mobile;
  2.  
  3. Import java. Text. simpledateformat;
  4. Import java. util. date;
  5. Import Android. App. activity;
  6. Import Android. OS. Bundle;
  7. Import Android. OS. systemclock;
  8. Import Android. View. view;
  9. Import Android. View. View. onclicklistener;
  10. Import Android. widget. Button;
  11. Import Android. widget. chronometer;
  12. Import Android. widget. textview;
  13. Import Android. widget. chronometer. onchronometerticklistener;
  14.  
  15. Public class main extends activity implements
    Onclicklistener, onchronometerticklistener
  16. {
  17. Private chronometer;
  18. Private textview tvtime;
  19. @ Override
  20. Public void onclick (view)
  21. {
  22. Switch (view. GETID ())
  23. {
  24. Case R. Id. btnstart:
  25. // Start Timer
  26. Chronometer. Start ();
  27. Break;
  28. Case R. Id. btnstop:
  29. // Stop the timer
  30. Chronometer. Stop ();
  31. Break;
  32. Case R. Id. btnreset
  33. // Reset the Timer:
  34. Chronometer. setbase (systemclock. elapsedrealtime ());
  35. Break;
  36. }
  37. }
  38. @ Override
  39. Public void onchronometertick (chronometer)
  40. {
  41. Simpledateformat SDF = new simpledateformat ("HH: mm: SS ");
  42. // Display the current time in the textview component
  43. Tvtime. settext ("current time:" + SDF. Format (new date ()));
  44. }
  45. @ Override
  46. Public void oncreate (bundle savedinstancestate)
  47. {
  48. Super. oncreate (savedinstancestate );
  49. Setcontentview (R. layout. Main );
  50. Tvtime = (textview) findviewbyid (R. Id. tvtime );
  51. Button btnstart = (button) findviewbyid (R. Id. btnstart );
  52. Button btnstop = (button) findviewbyid (R. Id. btnstop );
  53. Button btnreset = (button) findviewbyid (R. Id. btnreset );
  54. Chronometer = (chronometer) findviewbyid (R. Id. chronometer );
  55. Btnstart. setonclicklistener (this );
  56. Btnstop. setonclicklistener (this );
  57. Btnreset. setonclicklistener (this );
  58. // Set the timer listening event
  59. Chronometer. setonchronometerticklistener (this );
  60. // Set the timing information format
  61. Chronometer. setformat ("Timer: % s ");
  62. }
  63. }

Run the example in this section and click Start. The timing information is displayed at the bottom of the button, and the current time is displayed at the top of the button, as shown in Figure 8.16. After you click the reset button, the timing information at the bottom of the button is displayed starting from "Timer: 00: 00.

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.