简体   繁体   中英

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

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();
    }

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM