问题越来越定时器取消一次迭代和另一个开始工作后, [英] Problem getting timer to work after cancelling one iteration and starting another

查看:175
本文介绍了问题越来越定时器取消一次迭代和另一个开始工作后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持试图让我的定时器工作,在那里我会做一倒计时,然后回去做一套。

I am stuck trying to get my timer to work, where I will do one countdown, then go back and do another.

因此​​,在第二次调用 timer.scheduleAtFixedRate 我得到一个 IllegalStateException异常,但我取消的TimerTask 不是定时

So, on the second call to timer.scheduleAtFixedRate I get an IllegalStateException, but I am cancelling the TimerTask not the Timer.

我想要做的就是算打倒二十秒钟,然后十秒钟,再重复,每次更新一个文本框,告知用户他们应该做的事情。

What I want to do is count down twenty seconds, then ten seconds, then repeat, updating a text box each time, to inform the user what they should be doing.

一个问题是,倒计时不止一次/秒的速度更快。

One issue is that the countdown is faster than once/second.

04-22 01:34:57.118: DEBUG/TestHandler1(404): message called:2:null
04-22 01:34:57.709: DEBUG/TestHandler1(404): message called:3:null
04-22 01:34:57.899: DEBUG/TestHandler1(404): message called:4:null
04-22 01:34:58.198: DEBUG/TestHandler1(404): message called:5:null

下面是我的例外:

04-22 01:35:48.529: ERROR/AndroidRuntime(404): java.lang.IllegalStateException: TimerTask is scheduled already
04-22 01:35:48.529: ERROR/AndroidRuntime(404):     at java.util.Timer.scheduleImpl(Timer.java:574)
04-22 01:35:48.529: ERROR/AndroidRuntime(404):     at java.util.Timer.scheduleAtFixedRate(Timer.java:530)

下面是code我用这样的尝试。当我试图使用不同的TimerTask每个回路是更糟糕的行为,因为我把新的TimerTask 块之前刚刚 timer.scheduleAtFixedRate ,所以我又回到了这个版本。

Here is the code I used for this attempt. When I tried to use a different timertask for each loop it was even worse behavior, as I put the new TimerTask block just before timer.scheduleAtFixedRate, so I went back to this version.

    handler = new Handler() {
        public void handleMessage(Message msg) {
            counterText.setText((new Date()).toString() + "  "
                    + Integer.toString(cntr));
            System.out.println("handleMessage");
        }
    };

    timertask = new TimerTask() {
        public void run() {
            handler.sendEmptyMessage(0);
            cntr++;
            if (cntr > maxReps) {
                timertask.cancel();
                cntr = 0;
            }
        }
    };
    doneButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
                cntr = 0;
                for (MyClass mclass : input.getLoop()) {
                    labelText.setText(mclass.getName());
                    for (int y = 0; y < 8; y++) {
                        maxReps = 20;
                        timer.scheduleAtFixedRate(timertask, 0, 1000);
                        maxReps = 10;
                        labelText.setText("Rest");
                        timer.scheduleAtFixedRate(timertask, 0, 1000);
                    }
                }
        }
    });

希望有人能指出一个简单的错误我犯了。

Hopefully someone can point out a simple mistake that I made.

以上是从活动我的code的片段,所以我包括刚才我认为是需要显示在这里我有一个问题。

Above is a snippet of my code from the Activity, so I am including just what I think is needed to show where I am having a problem.

推荐答案

鉴于这篇文章的开发者指南中:的 http://developer.android.com/resources/articles/timed-ui-updates.html 以及类似的帖子在这里:的 http://cart.kolix.de/?p=1438 ,我建议你改变策略,并使用 handler.postDelayed(这一点,2000年);

Given this article in the dev guide: http://developer.android.com/resources/articles/timed-ui-updates.html and a similar post here: http://cart.kolix.de/?p=1438 , I would suggest you to change the strategy and use handler.postDelayed(this, 2000);

但是,具体到code,我的猜测是,它可能会取消错误的任务。我真的不明白的理由有两个呼叫 timer.scheduleAtFixedRate(TimerTask的,0,1000); 并设置 maxReps = 20; ,然后再 maxReps = 10; 在相同的循环,因为循环不会等待任务完成后再继续

But, specifically to the code, my guess is that it may get cancelled the wrong task. I don't really understand the rationale for having two calls to timer.scheduleAtFixedRate(timertask, 0, 1000); and setting maxReps = 20; first and then maxReps = 10; in the same loop, because the loop will not wait for the task to complete before continuing.

所以,你发送至少16呼叫 timer.scheduleAtFixedRate(),而不是都可以在正确的时间被取消。

So, you are sending at least 16 calls to timer.scheduleAtFixedRate() and not all may get cancelled at the right time.

这篇关于问题越来越定时器取消一次迭代和另一个开始工作后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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