首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >多个CountDown定时器一个接一个运行

多个CountDown定时器一个接一个运行
EN

Stack Overflow用户
提问于 2014-02-04 14:30:37
回答 2查看 3.7K关注 0票数 3

这是我的第一个问题。我需要在我的应用程序中实现一个接一个运行的countdown timers。当第一次完成时,下一次就开始了,等等。每次运行的Init time取决于用户的输入。问题是,对于每一个运行的onTick()onFinish()方法,我都需要在countdown timeronFinish()方法中放置不同的代码,而且,我不知道如何在完成一个counter之后启动下一个counter。我正在考虑在当前方法的counter方法中调用next onFinish(),但是我想不出如何用6 counters来实现这一点。

这是我的倒计时课:

代码语言:javascript
运行
复制
public class Counter extends CountDownTimer
{

    public Counter(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }

    public void onTick(long millisUntilFinished)
    {
            //this code is the same for every counter
            timer_view.setText(formatTime(millisUntilFinished));

            //this code depends on specific counter running
            output_view1.setText("My output here");
            output_view2.setText("My output here");
            output_view3.setText("My output here");

    }

    public void onFinish()
    {
        playSound(sound_id_1);
        runMyMethod(user_input_1);

        timerHasStarted = false;


    }

}

我在同一活动中启动我的柜台:

代码语言:javascript
运行
复制
if(!timerHasStarted)
{
counter = new Counter(user_input1, 1000);
    counter.start();
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-04 14:51:50

您可能需要打破开始计时器的功能,并在onFinish()中调用它。

代码语言:javascript
运行
复制
    public void startTimer(int counterId){
            Counter counter = null;
            switch(counterId){
                    case 0:
                            counter = new CounterOne(counterId,user_input1,1000);
                            break;  
                    /* Counter 1-5  goes here*/
                    default:
                            break;
            }
            if(counter !=null ){
                    counter.start();
            }
    }

然后在onFinsh()中启动下一个定时器

代码语言:javascript
运行
复制
    public abstract class Counter extends CountDownTimer
    {
            private int counterId;

            public Counter(int counterId /*counter id start with 0*/,long millisInFuture, long countDownInterval)
            {
                    super(millisInFuture, countDownInterval);
                    this.counterId = counterId;
            }

            public abstract void onTick(long millisUntilFinished);

            public void onFinish()
            {
                    playSound(sound_id_1);
                    runMyMethod(user_input_1);
                    startTimer(this.counterId++);
            }

    }

    public class CounterOne extends Counter{
            public void onTick(long millisUntilFinished)
            {
                    //counter 1 logic
            }
    }

    /* Subclass others. eg. CounterTwo etc. */
票数 1
EN

Stack Overflow用户

发布于 2014-02-04 16:08:28

你从来没有

代码语言:javascript
运行
复制
timerHasStarted

敬真。这总是错误的,so...yeah,一个接一个的计时器。在调用counter.start()之前将其设置为true,它应该可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21555209

复制
相关文章

相似问题

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