安卓:计时器任务查询 [英] Android: Timer Task query

查看:149
本文介绍了安卓:计时器任务查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中基于一些点击我开始使用一个TimerTask()计时器的应用程序。
但我也想有多个定时器的多次点击支持。
所以,如果一个定时器已经工作,并再次点击发出然后启动一个独立的计时器线程,而不仅仅是取消第一个。

可能有人请帮忙吗?

  @覆盖
公共无效onListItemClicked(INT指数,地图<弦乐,对象>的数据){
    定时器=新定时器();
    timer.schedule(新的TimerTask(){
         INT N = 0;
         @覆盖
         公共无效的run(){
             如果(++ñ== 300){
                 timer.cancel();
             }
             定时器= NULL;
         }
    },1000,1000);
}


解决方案

您可以有这样的事情:

  @覆盖
公共无效onListItemClicked(INT指数,地图<弦乐,对象>的数据){
    //你不应该有定时器作为类的属性
    //如果让你的计时器将取消本身,当你再次点击
    当n的数为300 //本地定时器将被取消只
    定时器定时器=新的Timer();
    timer.schedule(新的TimerTask(){
        INT N = 0;
        @覆盖
        公共无效的run(){            如果(++ñ== 300){
                timer.cancel();
            }
            定时器= NULL;
        }
    },1000,1000);
}

I have an app in which based on some click I start the timer using the TimerTask(). But I would also like to have support for multiple timers for multiple clicks. So if one timer is already working and another click is issued then it starts a separate timer thread and not just cancel the first one.

Could someone please help?

@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);
}

解决方案

You can have things like this:

@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);
}

这篇关于安卓:计时器任务查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆