[英]onTick method of Countdown timer keeps getting called even after calling cancel
I am trying to load some data. 我正在尝试加载一些数据。 Id data is loaded in 20 seconds than i start new activity else i will finish by giving some relevant message.
与开始新活动相比,将在20秒内加载ID数据,否则我将通过给出一些相关消息来结束。 I have started a countdownTimer to keep track of time.
我已经启动了一个countdownTimer来跟踪时间。 Once data is loaded, I want to stop the timer.
数据加载后,我想停止计时器。 I have Following class :
我有以下课程:
public class SplashActivity extends AppCompatActivity {
private Context mContext;
private Boolean mDataLoadedFromServer = false;
private String mJSONData;
private SplashTimerForLoadingMasterDataForAllChannels mTimer;
private void stopTimer(){
mTimer.cancel();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_splash);
mTimer = new SplashTimerForLoadingMasterDataForAllChannels(20000,1000);
mTimer.start();
}
class SplashTimerForLoadingMasterDataForAllChannels extends CountDownTimer {
public SplashTimerForLoadingMasterDataForAllChannels(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
Log.d("testTimer", "SplashTimerForLoadingMasterDataForAllChannels");
//SomE AsyncTAsk
LoadData loaddata = new LoadData();
loaddata.execute();
//SomE AsyncTAsk
}
@Override
public void onTick(long millisUntilFinished) {
Log.d("testTimer", "onTick millisUntilFinished = " + millisUntilFinished + " mDataLoadedFromServer = " + mDataLoadedFromServer);
//mDataLoadedFromServer is modified once Data is loaded in AsyncTask
if(mDataLoadedFromServer) {
stopTimer();
}
}
@Override
public void onFinish() {
Log.d("testTimer", "onFinish");
if(mDataLoadedFromServer) {
mDataSavedAndNextActivityLaunched = true;
if (Utils.checkIfUserLoggedIn()) {
mContext.startActivity(new Intent(mContext, ABCACtivity.class));
} else {
mContext.startActivity(new Intent(mContext, XYZActivity.class));
}
finish();
}
}
}
}
I cancel it in a local method call but onTick still keeps getting called. 我在本地方法调用中取消了它,但onTick仍然不断被调用。 Can someone please help?
有人可以帮忙吗?
Basically counter does not simply stop if I cancle it from onTick() or from onFinish() ie FROM INSIDE TIMER. 基本上,如果我从onTick()或onFinish()(即从内部计时器)取消计数器,计数器不会简单地停止。
However it stops very easily if I do it from any point outside of timer. 但是,如果我从计时器之外的任何位置进行操作,则停止非常容易。
So the point at which my data is fully loaded...I called timer.cancle() and it did the trick. 因此,我的数据被完全加载的那一刻……我调用了timer.cancle()并成功了。
However I still dont understand why it does not work if we do same from inside timer methods. 但是,我仍然不明白,如果我们从内部计时器方法中进行操作,为什么它不起作用。
I tried this code snippet, since most answers are saying you cannot cancel the timer inside its implementation, thus i tried using a handler inside onFinish. 我尝试了此代码段,因为大多数答案都说您无法在其实现内取消计时器,因此我尝试在onFinish内使用处理程序。 Old post but if anyone comes across this its helpful.
旧帖子,但如果有人碰到它,则很有帮助。
new Handler().post(new Runnable() {
@Override
public void run() {
timerTextView.setText("00:" + String.format("%02d", counter));
cancel();
}
});
you need to call stopTimer()
outside of the CountDownTimer
's onTick()
, 您需要在
CountDownTimer
的onTick()
之外调用stopTimer()
onTick()
,
something like this 像这样的东西
@Override
public void onFinish() {
Log.d("testTimer", "onFinish");
if(mDataLoadedFromServer) {
mDataSavedAndNextActivityLaunched = true;
if (Utils.checkIfUserLoggedIn()) {
mContext.startActivity(new Intent(mContext, ABCACtivity.class));
} else {
mContext.startActivity(new Intent(mContext, XYZActivity.class));
}
finish();
stopTimer();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.