我有一个应用程序,其中基于一些点击,我使用TimerTask()启动计时器。但我也希望有多个计时器的支持多次点击。因此,如果一个计时器已经在工作,并且发出了另一个点击,那么它将启动一个单独的计时器线程,而不仅仅是取消第一个线程。
有没有人能帮帮忙?
@Override
public void onListItemClicked(int index, Map<String, Object> data) {
timer = new Timer();
timer.schedule(new TimerTask() {
int n = 0;
@Override
public void run() {
if (++n == 300) {
timer.cancel();
}
timer = null;
}
},1000,1000);
}发布于 2013-04-10 18:17:38
你可以拥有这样的东西:
@Override
public void onListItemClicked(int index, Map<String, Object> data) {
//you shouldn't have timer as class' property
//if so your timer will cancel itself when you click again
//local timer will be cancelled when n is counted to 300 only
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int n = 0;
@Override
public void run() {
if (++n == 300) {
timer.cancel();
}
timer = null;
}
},1000,1000);
}https://stackoverflow.com/questions/15922813
复制相似问题