Kalkrin Kalkrin - 7 days ago 6
Android Question

Need android timer seconds to reset at 60

I am building an app which uses a timer. I have a simple timer with a start and stop button. It works but I want the seconds to reset at 60. Currently it keeps going 61, 62, 63 etc. Can I get some help with this? Below are my start method, stop method and the runnable which has my timer.

private long startTime = 0;
private long timeInMilliSeconds = 0;
private long updatedTime = 0;
private long timeSwapBuff = 0;

start.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
});

stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timeSwapBuff += timeInMilliSeconds;
customHandler.removeCallbacks(updateTimerThread);
}
});

private Runnable updateTimerThread = new Runnable() {
@Override
public void run() {
timeInMilliSeconds = SystemClock.uptimeMillis() - startTime;

updatedTime = timeSwapBuff + timeInMilliSeconds;

int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
int milliseconds = (int) (updatedTime % 1000);

timer.setText("" + mins + ":" + String.format("%02d", secs) +
":" + String.format("%02d", milliseconds));
customHandler.postDelayed(this, 0);
}
};

Answer

Instead of printing the actual timer value have you considering printing time%60 like this

int timeEllapsed = ... // the actual time spent for eg lets say 98
int timeToBePrinted = timeEllapsed%60 // the time you are going to show (38 seconds)