CountDownTimer cancel()不会取消计时器并继续工作

时间:2019-10-18 12:44:19

标签: java android firebase firebase-realtime-database countdowntimer

我正在使用CountDownTimer类更新Firebase实时数据库,但是cancel()方法似乎不起作用。

我想在计时器结束时调用fetchUserPoints()并添加点本身。

可以帮我吗?

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) {

            }
        });
    }

,然后fetchUserPoints()继续调用。

3 个答案:

答案 0 :(得分:1)

您可以只在onFinish方法中取消CountDownTimer。试试这个

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) {

            }
        });
    }

另一个示例,在这里看到它正常工作

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

答案 1 :(得分:0)

尝试将 CountDownTimer 保留为方法的局部变量而不是全局/类级别的变量,因为,如果您触发 timeDownTimer()< / em> 多次,则 countDownTimer 的旧参考将丢失,并且您将无法取消以前启动的计时器。

我认为这可能是个问题。

答案 2 :(得分:0)

您无需在 fetchUserPoints()中调用 countDownTimer.cancel(),因为您在内部调用了 fetchUserPoints() countDownTimer 的onFinish 方法;您正在尝试取消结束的过程。