CountDownTimer cancel() doesn't cancel the timer and continue working

countdowntimer cancel not working
how to stop countdown timer in android
countdowntimer android
how to start and stop timer in android
android countdown timer run in background
how to show timer in android
simple countdown timer in java
countdown timer android app

I'm using CountDownTimer class to update firebase realtime database, but cancel() method seems doesn't work.

I want when the timer finishes to call fetchUserPoints() and to add the points itself.

Could you help me, please?

int time = 6;
private CountDownTimer countDownTimer;


private void timeDownTimer() {
        countDownTimer = new CountDownTimer(6000, 1000) {

            public void onTick(long millisUntilFinished) {
                tvTimer.setText("0:" + checkDigit(time));
                time--;

            }

            public void onFinish() {
                tvTimer.setText("Time finished");
                fechUserPoints();
            }


        }.start();
    }

    public String checkDigit(int number) {
        return number <= 9 ? "0" + number : String.valueOf(number);
    }

    private void fetchUserPoints() {
        countDownTimer.cancel();
        reference.child(mId).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                prevPoints = dataSnapshot.child("points").getValue().toString();
                int prePoints = Integer.valueOf(prevPoints);
                String newPoints = String.valueOf(prePoints + ADDED_POINTS);

                reference.child(mId).child("points").setValue(newPoints).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(getContext(), "Process is successful", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

and here fetchUserPoints() continue calling.

You can just cancel your CountDownTimer inside onFinish method. Try this

int time = 6;
private CountDownTimer countDownTimer;


private void timeDownTimer() {
        countDownTimer = new CountDownTimer(6000, 1000) {

            public void onTick(long millisUntilFinished) {
                tvTimer.setText("0:" + checkDigit(time));
                time--;

            }

            public void onFinish() {
                tvTimer.setText("Time finished");
                countDownTimer.cancel();
                fechUserPoints();
            }


        }.start();
    }

    public String checkDigit(int number) {
        return number <= 9 ? "0" + number : String.valueOf(number);
    }

    private void fetchUserPoints() {

        reference.child(mId).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                prevPoints = dataSnapshot.child("points").getValue().toString();
                int prePoints = Integer.valueOf(prevPoints);
                String newPoints = String.valueOf(prePoints + ADDED_POINTS);

                reference.child(mId).child("points").setValue(newPoints).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(getContext(), "Process is successful", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

Another example see here it working

private CountDownTimer countDownTimer;
    int time = 6;

    private void timeDownTimer() {
        countDownTimer = new CountDownTimer(6000, 1000) {

            public void onTick(long millisUntilFinished) {
                Log.e("TIK : " , checkDigit(time));
                time--;

            }

            public void onFinish() {
                countDownTimer.cancel();
                test();
            }


        }.start();
    }

    public String checkDigit(int number) {
        return number <= 9 ? "0" + number : String.valueOf(number);
    }

    private void test(){
        Log.e("TIK : " , "inside my method");
        Toast.makeText(this,"on finish",Toast.LENGTH_LONG).show();
    }

How to stop CountDownTimer in android, You can assign it to a variable and then call cancel() on the variable CountDownTimer yourCountDownTimer = new CountDownTimer(zaman,  CountDownTimer has a cancel() method that can be called to terminate the timer. Your MainActivity would look like this public class MainActivity extends Activity { private final CounterClass timer; @Override public void onCreate() { timer = new CounterClass(10999, 900); timer.start(); /* rest of the code omitted */ } @Override public

Try to maintain CountDownTimer as a method local variable and not as a global/class level variable, because if you trigger timeDownTimer() multiple time then the old reference of countDownTimer would be lost and you would not be able to cancel previous timers which were initiated.

I think that might can be a problem here.

How do you cancel CountDownTimer?, You appear to be calling cancel on the Dialog, but never on the timer. You need to maintain a reference to the CountDownTimer, and call its cancel method in your onClick method. Jun 29, 2010. I'm extending the CountDownTimer class to obtain some custom functionality .In onTick() in case some conditions are met I call cancel() , expecting that will be the end of it, however the onTick() callback gets call until the the count down is reached .

You don't need to call countDownTimer.cancel() inside fetchUserPoints() since you call fetchUserPoints() inside onFinish method of countDownTimer; You are trying to cancel an ended process.

CountDownTimer, CountDownTimer.cancel (Showing top 20 results out of 315). Common ways to obtain CountDownTimer. private void myMethod () {. CountDownTimer c =. CountDownTimer has a cancel() method that can be called to terminate the timer. Your MainActivity would look like this public class MainActivity extends Activity { private final CounterClass timer; @Override public void onCreate() { timer = new CounterClass(10999, 900); timer.start(); /* rest of the code omitted */ } @Override public

android.os.CountDownTimer.cancel java code examples, So I have to call cancel. but when calling cancel it doesn't work. please check printStackTrace(); } } @Override public void onFinish() { try { if  Countdown Timer is just the countdown taken out of the usual online-stopwatch.com stopwatch and countdown gadget. It makes it easier to get to the countdown – Much better if the countdown is the only part you ever use.

How to make a countdown timer in Android?, The relevant methods of the CountDownTimer Class are given below. synchronized final void cancel() : This is used to cancel the countdown; abstract void  What do you mean by, "the timer doesn't stop and counting very fast."? Samira Radwan 5-Apr-18 7:21am the problem is if i don't call (cancel) the timer counts down very fast and doesn't stop. it closes the video after 20 seconds but it keeps running

How to Create a Timer in Java, Cancel the countdown. *. * Do not call it from inside CountDownTimer threads. */. public final void cancel() {. mHandler.removeMessages(MSG);. mCancelled  Big Countdown that doesn’t sucks Demo & Down Posted on August 25, 2012 May 9, 2020 by agurchand Have you ever tried this awesome jQuery countdown timer script with your web application?

Comments
  • It should work. I've tested with another example, you can check my edited answer.
  • Here the test method will be called once on Finish of CountDownTimer
  • Thank you so muck bro, I figured out that fetchUserPoints() is the problem, your answer helped a lot, I really appreciate that.
  • Happy to help you and if you find this answer helpful please accept my answer. :)
  • Okay I did, but where exactly the right place to cancel the timer?
  • you can cancel in the same place where you are currently cancelling fetchUserPoints().
  • Thank you so muck bro, I figured out that fetchUserPoints() is the problem, I really appreciate your help.
  • but the method doesn't stop to be called.
  • Is the countdowntimer stopped or it restart like a loop?
  • Thank you so muck bro, I figured out that fetchUserPoints() is the problem, I really appreciate your help.