user2613996
user2613996

Reputation: 117

Previous state of timer is not cleared

I am trying to update a text view with time elapsed since a button was pressed, following is my code:

      public void startTimeCounter() {
        tripTimeCounter = new CountDownTimer(60 * 1000, 1000) {

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            repeatCounter = repeatCounter + 1;
            startTimeCounter();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            timedisplay = (TextView) findViewById(R.id.textView3);
            timedisplay.setText(formatInterval((repeatCounter * 60) * 1000
                    - millisUntilFinished));

        }

    }.start();
}

private static String formatInterval(final long l) {
    final long hr = TimeUnit.MILLISECONDS.toHours(l);
    final long min = TimeUnit.MILLISECONDS.toMinutes(l
            - TimeUnit.HOURS.toMillis(hr));
    final long sec = TimeUnit.MILLISECONDS.toSeconds(l
            - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min));
    final long ms = TimeUnit.MILLISECONDS.toMillis(l
            - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min)
            - TimeUnit.SECONDS.toMillis(sec));
    return String.format("%02d:%02d:%02d", hr, min, sec, 0);
}

The timer works perfectly, however when I press the button for the second time the value in the text view is not cleared, but instead a new time is associated which runs in parallel with the previous timer, I can see the previous time and a reflection of the new time incrementing simultaneously . I want to know as to how will I clear the previous state, before a second call is made to startTimeCounter();

The button Click Code:

   private View.OnClickListener but = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (isOnline() == true) {
            // PHP();
            // network();

            startTimeCounter();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Not connected to the internet", Toast.LENGTH_LONG)
                    .show();
        }
    }
};

Upvotes: 0

Views: 115

Answers (3)

Guillem
Guillem

Reputation: 126

Try this code:

public class XXX {
private CountDownMine CountDown;

private class CountDownMine extends CountDownTimer 
{
    public CountDownMine(long millisInFuture, long countDownInterval) 
    {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() 
    {
        //The same yours
    }

    @Override
    public void onTick(long millisUntilFinished) 
    {
        //The same yours
    }   
}


private View.OnClickListener but = new View.OnClickListener() 
{
    @Override
public void onClick(View v) 
{
        if (isOnline() == true) 
        {
            //Code  

    if(CountDown != null)
        CountDown.cancel();

    CountDown = new CountDownMine(60 * 1000, 1000);
    CountDown.start();
    } 
    else 
    {
        //Code
    }
    }
};

}

Upvotes: 1

rocketboy
rocketboy

Reputation: 9741

       public void onFinish() {
            // TODO Auto-generated method stub
            repeatCounter = repeatCounter + 1;
            startTimeCounter();
        }

You are creating a new counter every time previous one finishes or the button is clicked. That is the problem. Keep the CountDownTimer instance outside your startTimeCounter method and try passing it to startTimeCounter(CountDownTimer tripTimeCounter) and restart the timer onFinish() instead,

        public void onFinish() {
            // TODO Auto-generated method stub
            repeatCounter = repeatCounter + 1;
            this.start();
            }

EDIT:

private View.OnClickListener but = new View.OnClickListener() {

        tripTimeCounter = new CountDownTimer(60 * 1000, 1000) {

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            repeatCounter = repeatCounter + 1;
            startTimeCounter();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            timedisplay = (TextView) findViewById(R.id.textView3);
            timedisplay.setText(formatInterval((repeatCounter * 60) * 1000
                    - millisUntilFinished));

        }

    };

    @Override
    public void onClick(View v) {
        if (isOnline() == true) {
            // PHP();
            // network();

            tripTimeCounter.start();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Not connected to the internet", Toast.LENGTH_LONG)
                    .show();
        }
    }
};

Upvotes: 0

kiheru
kiheru

Reputation: 6618

You are creating a new CountDownTimer every time startTimeCounter is called. Restart the previous one instead:

// Initializing the timer earier
CountdownTimer tripTimeCounter = new CountDownTimer(60 * 1000, 1000) {
    ...
}

The timer starting method becomes:

public void startTimeCounter() {
    tripTimeCounter.cancel();
    tripTimeCounter.start();
}

Upvotes: 0

Related Questions