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:
- 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:
- Package net. blogjava. Mobile;
-
- Import java. Text. simpledateformat;
- Import java. util. date;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Import Android. OS. systemclock;
- Import Android. View. view;
- Import Android. View. View. onclicklistener;
- Import Android. widget. Button;
- Import Android. widget. chronometer;
- Import Android. widget. textview;
- Import Android. widget. chronometer. onchronometerticklistener;
-
- Public class main extends activity implements
Onclicklistener, onchronometerticklistener
- {
- Private chronometer;
- Private textview tvtime;
- @ Override
- Public void onclick (view)
- {
- Switch (view. GETID ())
- {
- Case R. Id. btnstart:
- // Start Timer
- Chronometer. Start ();
- Break;
- Case R. Id. btnstop:
- // Stop the timer
- Chronometer. Stop ();
- Break;
- Case R. Id. btnreset
- // Reset the Timer:
- Chronometer. setbase (systemclock. elapsedrealtime ());
- Break;
- }
- }
- @ Override
- Public void onchronometertick (chronometer)
- {
- Simpledateformat SDF = new simpledateformat ("HH: mm: SS ");
- // Display the current time in the textview component
- Tvtime. settext ("current time:" + SDF. Format (new date ()));
- }
- @ Override
- Public void oncreate (bundle savedinstancestate)
- {
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Tvtime = (textview) findviewbyid (R. Id. tvtime );
- Button btnstart = (button) findviewbyid (R. Id. btnstart );
- Button btnstop = (button) findviewbyid (R. Id. btnstop );
- Button btnreset = (button) findviewbyid (R. Id. btnreset );
- Chronometer = (chronometer) findviewbyid (R. Id. chronometer );
- Btnstart. setonclicklistener (this );
- Btnstop. setonclicklistener (this );
- Btnreset. setonclicklistener (this );
- // Set the timer listening event
- Chronometer. setonchronometerticklistener (this );
- // Set the timing information format
- Chronometer. setformat ("Timer: % s ");
- }
- }
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.