问题
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?
回答1:
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();
}
};
来源:https://stackoverflow.com/questions/5345697/chronometer-reset