gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:24:15 24 4
gpt4 key购买 nike

我正在使用 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() 继续调用。

最佳答案

您可以在 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();
}

关于java - CountDownTimer cancel() 不会取消计时器并继续工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58451214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com