Shimrit Hadadi Shimrit Hadadi - 7 months ago 9
Java Question

How do i reset the timer to start from 00:00:00 each time i run my program over again?

I added to MainActvitiy.java

private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;


Then in the onCreate i added:
And did that the timer will start automatic when i run the program without the need to click the start button i want the timer to start right when i run my program.

customHandler.postDelayed(updateTimerThread,0);

timerValue = (TextView) findViewById(R.id.timerValue);

startButton = (Button) findViewById(R.id.startButton);

startButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);

}
});

pauseButton = (Button) findViewById(R.id.pauseButton);

pauseButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);

}
});


Then the method updateTimerThread:

private Runnable updateTimerThread = new Runnable() {

public void run() {

timeInMilliseconds = 0L;//SystemClock.uptimeMillis() - startTime;
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;

int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}

};


In this method i did:

timeInMilliseconds = 0L;


But it didn't change much.
what i want to do is each time i run my program from the beginning the timer will start from 00:00:00

EDIT
In the on activity i did now this:

startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread,0);
timerValue = (TextView) findViewById(R.id.timerValue);

startButton = (Button) findViewById(R.id.startButton);

startButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);

}
});

pauseButton = (Button) findViewById(R.id.pauseButton);

pauseButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);

}
});


In the upadteTimerThread i didn't change:

private Runnable updateTimerThread = new Runnable() {

public void run() {

timeInMilliseconds = System.currentTimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;

int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}

};


Still when running the program the timer is not starting from 00:00:00 but i see on the minutes a long number also in second like it's continuing not starting over like reseted.

Answer

In the startButton onClick method, you have:

startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);

But at the top, you only have:

customHandler.postDelayed(updateTimerThread,0);

Since updateTimerThread uses the startTime value, you'd very likely want to initialize it the same way at the top.