Question

when I clicked start, the 'stopwatch' started to count.. I pressed pause, only the textview paused, it's not resuming, because it still running in background.. I need it to be resumed at where I've stopped..

here's my dot java code :

package com.example.chronometer2;

import com.example.chronometer.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;

public class ChronometerApp extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_chronometer);

       final Chronometer myChronometer = (Chronometer)findViewById(R.id.my_chronometer);
       Button buttonStart = (Button)findViewById(R.id.buttonstart);
       Button buttonStop = (Button)findViewById(R.id.buttonstop);
       Button buttonReset = (Button)findViewById(R.id.buttonreset);

       buttonStart.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    myChronometer.start();
   }});

       buttonStop.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    myChronometer.stop();

   }});

       buttonReset.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    myChronometer.setBase(SystemClock.elapsedRealtime());

   }});

   }
}
Was it helpful?

Solution

You're just ought to remember the time, which has gone in variable, for example, timeWhenStopped. And then, each time when you start your chronometer, you start with value not 0, but with some seconds? which you can set by method setBase. Here 3 buttons with actions Start, Pause and Restart. Good luck!

public class MainActivity extends Activity {
/** Called when the activity is first created. */
long timeWhenStopped = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button butStart = (Button) findViewById(R.id.button1);
    final Button butStop = (Button) findViewById(R.id.button2);
    final Button butReset = (Button) findViewById(R.id.button3);

    final Chronometer Mchronometer = (Chronometer) findViewById(R.id.chronometer1);

    butStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {                  Mchronometer.setBase(SystemClock.elapsedRealtime()
                            +timeWhenStopped);
            Mchronometer.start();
        }
    });

    butStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            timeWhenStopped = Mchronometer.getBase() - SystemClock.elapsedRealtime();
            Mchronometer.stop();
        }
    });

    butReset.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Mchronometer.setBase(SystemClock.elapsedRealtime());
            Mchronometer.stop();
            timeWhenStopped = 0;
        }
    });
    }

OTHER TIPS

You need to define a variable which contains the elapsed time when the chronometer was stopped

timeWhenStopped = chronometer.getBase() - SystemClock.elapsedRealtime();

and then when you start again set the the timer plus the timeWhenStopped

chronometer.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);

to reset the chronometer just use

chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.stop();
timeWhenStopped = 0;

and maybe you need to add an extra variable to define if the chronometer is running

You are gonna need a variable that keeps track on the time that has passed since the Chronometer was started

See more : Android_Chronometer pause

@fabzy's code works, but the arithmetic logic doesn't, which can cause problems.

mTimeWhenStopped should be a positive number (the amount of elapsed time on the timer), but in fabzy's setup, it's always negative. This makes a difference if you're using the mTimeWhenStopped number for anything other than running the timer, like comparison, display, etc.

The following is the same code with the subtraction arithmetic reversed. I've also added an optional boolean for whether the timer is running or not:

private void resumeTimer() {
    mTimer.setBase(SystemClock.elapsedRealtime() - mTimeWhenStopped);
    mTimer.start();
    mTimerIsRunning = true;

}

private void stopTimer() {
    mTimer.stop();
    mTimeWhenStopped = SystemClock.elapsedRealtime() - mTimer.getBase();
    mTimerIsRunning = false;
}

private void resetTimer() {
    mTimer.stop();
    mTimer.setBase(SystemClock.elapsedRealtime());
    mTimeWhenStopped = 0;
    mTimerIsRunning = false;
}

I made a PauseableChronometer class for this.

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.Chronometer;

public class PausableChronometer extends Chronometer {

    private long timeWhenStopped = 0;

    public PausableChronometer(Context context) {
        super(context);
    }

    public PausableChronometer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PausableChronometer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void start() {
        setBase(SystemClock.elapsedRealtime()+timeWhenStopped);
        super.start();
    }

    @Override
    public void stop() {
        super.stop();
        timeWhenStopped = getBase() - SystemClock.elapsedRealtime();
    }

    public void reset() {
        stop();
        setBase(SystemClock.elapsedRealtime());
        timeWhenStopped = 0;
    }

    public long getCurrentTime() {
        return timeWhenStopped;
    }

    public void setCurrentTime(long time) {
        timeWhenStopped = time;
        setBase(SystemClock.elapsedRealtime()+timeWhenStopped);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top