首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:定时任务查询

Android:定时任务查询
EN

Stack Overflow用户
提问于 2013-04-10 18:05:29
回答 1查看 1.3K关注 0票数 1

我有一个应用程序,其中基于一些点击,我使用TimerTask()启动计时器。但我也希望有多个计时器的支持多次点击。因此,如果一个计时器已经在工作,并且发出了另一个点击,那么它将启动一个单独的计时器线程,而不仅仅是取消第一个线程。

有没有人能帮帮忙?

代码语言:javascript
复制
@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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-10 18:17:38

你可以拥有这样的东西:

代码语言:javascript
复制
@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);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15922813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档