[英]How do i cancel a timeout if a button is pressed?
I have a layout with some buttons, and I want after one of them is pushed to cancel the timeout, with the code that I have now I press one of the buttons and it redirects me to another activity but the timer dons't stop and after the 3 seconds it redirects me again, how do I cancel the timer if one of the buttons is pressed? 我有一个带有一些按钮的布局,我想在按下其中一个按钮以取消超时之后,使用现在具有的代码按一下按钮之一,它将我重定向到另一个活动,但是计时器不会停止,并且3秒后,它再次重定向我,如果按下其中一个按钮,如何取消计时器? This is the timer:
这是计时器:
int timeout = 3000; seconds Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { finish(); Intent homepage = new Intent(Act1.this, Act2.class); startActivity(homepage); } }, timeout);
Check the documentation and you will find a Timer.cancel()
. 查看文档 ,您会发现
Timer.cancel()
。 Keep a reference to the timer and call that whenever you want to make it stop. 保留对计时器的引用,并在要使其停止时调用它。
void cancel()
无效cancel()
Terminates this timer, discarding any currently scheduled tasks.
终止此计时器,放弃任何当前计划的任务。
Example: 例:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTimer.cancel();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.