Shimrit Hadadi
Shimrit Hadadi

Reputation: 115

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.

Upvotes: 0

Views: 387

Answers (2)

TheLandolorien
TheLandolorien

Reputation: 597

The key to resetting the timer back to zero is in the updatedTime variable. This is what determines where the timer starts when you press the Start button.

There's no need to reinitialize the startTime variable since startTime = SystemClock.uptimeMillis(); already properly sets the startTime back to 0. Remember that startTime is relative to what's currently showing on the timer. That's why the timer starts off where you paused it and doesn't skip the seconds for when the timer was paused.

Set the timeSwapBuff back to 0L in the onClick event for the Start Button. This resets the time buffer to 0. That then gets added back to the startTime (also 0) and forces the timer to start over completely.

Try:

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

Upvotes: 0

Andreas
Andreas

Reputation: 159155

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.

Upvotes: 2

Related Questions