gpt4 book ai didi

java - 使用 CountDownTimer 在特定时间在 Firestore 上设置特定数据

转载 作者:行者123 更新时间:2023-12-04 10:00:29 24 4
gpt4 key购买 nike

您好,我想在特定时间使用 countDownTimer 在 firestore 上设置特定数据。当我的倒计时完成时,数据在 firestore 上设置良好,但每次 Activity 更新时,数据都会一次又一次地重置。
当我的数据设置一次时,请有一种可能的方法来破坏完成方法吗?。如果您有其他建议表示欢迎

    firebaseAuth=FirebaseAuth.getInstance();
firebaseFirestore=FirebaseFirestore.getInstance();

long mil=200000;

downTimer =new CountDownTimer(mil,1000) {
@Override
public void onTick(long millisUntilFinished) {

}

@Override
public void onFinish() {

Map<String,Object> map=new HashMap<>();
map.put("Password","userPassword");

firebaseFirestore.collection("UsersName").document().set(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {

if (task.isSuccessful()) {

Toast.makeText(ForgetPasswordActivity.this, "Done sir", Toast.LENGTH_SHORT).show();
downTimer.cancel();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {

e.printStackTrace();
}
});

}
}.start();

}

}

最佳答案

每次您的 Activity 重新启动时,都会创建一个新的计时器实例,并且只要您的 Activity 还活着。

如果您需要一个可以在 Activity 生命周期内存活的计时器,您应该将它放在前台(或后台)服务或类似的服务中。

因为您使用的是 firebase,所以另一个解决方案是云函数。

关于java - 使用 CountDownTimer 在特定时间在 Firestore 上设置特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61842883/

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