Check if the chronometer is working

chronometer in android, how to check if the chronometer is working or stops? if you start, then I want to stop it, but if you don’t start, then start the chronometer.

+5
source share
4 answers

You can check this with the boolean variable. When you start the chronometer, you set the boolean variable true and when it stops setting boolean variable false.

boolean isChronometerRunning = false;
if (true)  // condition on which you check whether it start or stop
{
    chronometer.start();
    isChronometerRunning  = true;
}
else
{
  chronometer.stop();
  isChronometerRunning  = false;
}
+7
source

You can extend Chronomter, for example:

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

public class MyChronometer extends Chronometer {

    private boolean isRunning = false;

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

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

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

    @Override
    public void start() {
        super.start();
        isRunning = true;
    }

    @Override
    public void stop() {
        super.stop();
        isRunning = false;
    }

    public boolean isRunning() {
        return isRunning;
    }

}

And then just call isRunning().

+5
source

, . , .

You can simply take the source code for this class, implement it in your project yourself and add a method like this:

public boolean getStarted() {
    return mStarted;
}
+2
source
private boolean isChronometerRunning = false;

private Chronometer chronometer;

chronometer = (Chronometer) findViewById(R.id.chronometer);

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

isChronometerRunning = true;

Now that you want to stop the chronometer, use the code below to check the chronometer.

    if (isChronometerRunning){
        chronometer_call.stop();
    }
0
source

All Articles