About chronometer (timer) pause

Source: Internet
Author: User

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 ());}


 

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.