Android - Get time of chronometer widget Android - Get time of chronometer widget android android

Android - Get time of chronometer widget


If you look at the source of the Chronometer class, you'll see that it doesn't store the elapsed time in a field and it calculates it internally every time it needs to update the display.

However it's relatively easy to do the same in your own code:

long elapsedMillis = SystemClock.elapsedRealtime() - chronometerInstance.getBase();

This assumes that you have started your clock something like this:

chronometerInstance.setBase(SystemClock.elapsedRealtime());chronometerInstance.start();

Here's a full example:

public class ChronoExample extends Activity {Chronometer mChronometer;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    LinearLayout layout = new LinearLayout(this);    layout.setOrientation(LinearLayout.VERTICAL);    mChronometer = new Chronometer(this);    layout.addView(mChronometer);    Button startButton = new Button(this);    startButton.setText("Start");    startButton.setOnClickListener(mStartListener);    layout.addView(startButton);    Button stopButton = new Button(this);    stopButton.setText("Stop");    stopButton.setOnClickListener(mStopListener);    layout.addView(stopButton);    Button resetButton = new Button(this);    resetButton.setText("Reset");    resetButton.setOnClickListener(mResetListener);    layout.addView(resetButton);            setContentView(layout);}private void showElapsedTime() {    long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();                Toast.makeText(ChronoExample.this, "Elapsed milliseconds: " + elapsedMillis,             Toast.LENGTH_SHORT).show();}View.OnClickListener mStartListener = new OnClickListener() {    public void onClick(View v) {        mChronometer.start();        showElapsedTime();    }};View.OnClickListener mStopListener = new OnClickListener() {    public void onClick(View v) {        mChronometer.stop();        showElapsedTime();    }};View.OnClickListener mResetListener = new OnClickListener() {    public void onClick(View v) {        mChronometer.setBase(SystemClock.elapsedRealtime());        showElapsedTime();    }};}

One somewhat confusing thing about Chronometer is that you can't really use it as a stopwatch that gets started, stopped and restarted again. When it's running, it will always show the time elapsed since you last reset it, no matter how many times and for how long you have stopped it in the meantime. When it is stopped, it simply stops updating the display.

If you need something like a stopwatch you'll have to subclass Chronometer or maybe create your own version using the source.

alt text


I found this example really useful, thanks nyenyec!

Here's my two cents on how to turn it into a real stopwatch function, without subclassing Chronometer. Just change the mStartListener method to parse the text from mChronometer (it's derived from TextView after all), calculate milliseconds, and use setBase() to readjust the base time to that amount of time in the past:

  View.OnClickListener mStartListener = new OnClickListener() {    public void onClick(View v) {      int stoppedMilliseconds = 0;      String chronoText = mChronometer.getText().toString();      String array[] = chronoText.split(":");      if (array.length == 2) {        stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000            + Integer.parseInt(array[1]) * 1000;      } else if (array.length == 3) {        stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000             + Integer.parseInt(array[1]) * 60 * 1000            + Integer.parseInt(array[2]) * 1000;      }      mChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);      mChronometer.start();    }  };


@nyenyec +1: here is what i ended up with, while using nyenyec's response without a sub class.

        chronometer.setOnChronometerTickListener(new OnChronometerTickListener() {                              @Override        public void onChronometerTick(Chronometer chronometer) {            long elapsedMillis = SystemClock.elapsedRealtime() - chronometer.getBase();            if(elapsedMillis>THRESHOLD){                doYourStuff();            }        }    });

where THRESHOLD is

private static final int THRESHOLD_EXERSISE = 60000; //In milliseconds