Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chronometer reset

I am trying to completely restart Chronometer and its does not work. Instead it is being paused. Basically what I am trying to do is to do something while chronometer is counting till 10. After its done we prompt the user to try again. In which case we want to redo the count from 1 to 10 sec. But the Chronometer starts from the paused time instead of starting 0.

here is the code:

_cutOfTime = 10; // constant

every time button is pressed do startRecording()

it should always initiate the Chronometer instead of stop/pause it, but it does the opposite

protected void startRecording(){    

this._watch = (Chronometer) findViewById(R.id.chrono);

            if (this._watch != null)
                 this._watch.setOnChronometerTickListener(new OnChronometerTickListener() {

                        @Override
                        public void onChronometerTick(Chronometer chronometer) {
                            long countUp = (SystemClock.elapsedRealtime() - chronometer.getBase()) / 1000;
                            Log.i(_tag, "time now: " + String.valueOf(countUp));

                            if(countUp > _cutOfTime)
                            {
                                Log.i(_tag, "stop recording!!: ");
                                 _watch.stop();
                                stopRecordWav();
                                launchPromptWithResults();
                            }
                            long sec = countUp % 60;

                            String asText = "" + sec;
                            _textView.setText("Recorded: " + asText);
                        }
                    });


            if (_watch != null)
            _watch.start();
}

Is there a way to reset the chronometer so it does not pause but completely stop?

like image 860
dropsOfJupiter Avatar asked Mar 17 '11 21:03

dropsOfJupiter


People also ask

How do I turn off chronometer on Android?

To stop the chronometer, use the stop method. The start and stop methods do not change the base, they simply start and stop the display of the count in the text view. That is, once started, the Chronometer never stops counting. However, you can reset the base by invoking setBase with a new starting point.

How do you find the time from a chronometer?

getText(). toString() gives you the total elapsed time in seconds (my application is some kind of stop watch, so you can pause it and resume it). Hope it helps.


1 Answers

When I played with the chronometer awhile back I just used the setBase() method to set the base to the current time just before calling start(). Depending on your exact needs you may need to add some logic around whether to reset the chronometer or not before starting it.

View.OnClickListener mStartButtonListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            mChronometer.setBase(SystemClock.elapsedRealtime());
            mChronometer.start();
        }
    };
like image 198
jmichalicek Avatar answered Sep 20 '22 15:09

jmichalicek