Problem description:
When the stop () method is used to pause the timer, when the START () method is used to continue timing, the timer display time is not the time before the pause.
Problem Analysis:
You can view the official API documentation and find:
PublicvoidStop()
Added in API Level 1
Stop countingup. this does not affect the base as set from setbase (long), just the viewdisplay. this stops the messages to the handler, effectively releasing resourcesthat wocould be held as the chronometer is running, via START ().
The stop () method only stops the display of the time when the timer is refreshed, but does not actually stop the timer. When the stop () method is called, the timer is still timing, but the interface is no longer refreshed.
In this way, how can the timer resume timing from the time before the pause?
There are two ways to solve this problem:
Method 1:
Start Time and recovery time:
// Set the time to the time when the timer is paused. setbase (convertstrtimetolong (chronometer. gettext (). tostring (); chronometer. Start (); // start the timer
Pause timing:
Chronometer. Stop (); // stop timing
Get the display time before pause and convert it to long type time:
/*** Converts the Time of the string type to long, for example, 12: 01: 08 * @ Param strtime string type time * @ return long type time */protected long convertstrtimetolong (string strtime) {// todo auto-generated method stub string [] timearry = strtime. split (":"); long longtime = 0; If (timearry. length = 2) {// if the time is mm: SS format longtime = integer. parseint (timearry [0]) * 1000*60 + integer. parseint (timearry [1]) * 1000;} else if (timearry. length = 3) {// if the time is hh: mm: SS format longtime = integer. parseint (timearry [0]) * 1000*60*60 + integer. parseint (timearry [1]) * 1000*60 + integer. parseint (timearry [0]) * 1000;} return systemclock. elapsedrealtime ()-longtime ;}
Method 2:
Private chronometer recordchronometer; private long recordingtime = 0; // total recorded time public void onrecordstart () {recordchronometer. setbase (systemclock. elapsedrealtime ()-recordingtime); // skip the recorded time to continue timing. start ();} public void onrecordpause () {recordchronometer. stop (); recordingtime = systemclock. elapsedrealtime ()-recordchronometer. getbase (); // time when the record was saved} public void onrecordstop () {recordingtime = 0; recordchronometer. setbase (systemclock. elapsedrealtime ());}