简体   繁体   English

如何在CountDownTimer内定期调用方法?

[英]How can I call a method at regular intervals inside of a CountDownTimer?

What my code does: My activity has a CountDownTimer that starts when a user presses a button. 我的代码做什么:我的活动有一个CountDownTimer ,它在用户按下按钮时启动。 When it completes, a sound is played. 完成后,将播放声音。 Here's the code: 这是代码:

public class PrepTimer extends CountDownTimer {
    public PrepTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        updateSessionRemaining(millisUntilFinished);
        setPrepDigits(millisUntilFinished);
    }

    @Override
    public void onFinish() {
        session.setPrepRemaining(0);
        playSound();
    }
}

What I'd like it to do: I'd like the sound to play at regular intervals over the course of the timer (in addition to at the end). 我想要做的是:我希望声音在计时器的过程中定期播放(除了结束时)。 For example, in a ten minute timer, the sound might play every 60 seconds. 例如,在十分钟的计时器中,声音可能每60秒播放一次。

Things I've tried: 我尝试过的事情:

  • Using an if statement inside the onTick method to check when millisUntilFinished is equal to a certain value (a multiple of 60 seconds, for example) and then running the method. onTick方法内使用if语句检查millisUntilFinished是否等于某个值(例如60秒的倍数),然后运行该方法。 This seems like the most straightforward solution, but I've found that the method is not triggered consistently (perhaps millisUntilFinished is skipping over the value I'm checking it against?). 这似乎是最直接的解决方案,但我发现该方法未持续触发(也许millisUntilFinished跳过了我要检查的值?)。
  • Creating separate, nested CountDownTimers and repeating with a for loop. 创建单独的嵌套CountDownTimers并使用for循环重复。 The problem with this is that the code quickly becomes overly complicated and my intuition tells me that I shouldn't be running timers within timers. 问题是代码很快变得过于复杂,我的直觉告诉我,我不应该在计时器中运行计时器。

Question: How can I run a method at regular intervals over the course of a CountDownTimer? 问题:如何在CountDownTimer的过程中定期运行方法?

Instead of using a countdown timer you can simply use a post delayed Handler and thread.At the end of the method post the handler with specified time interval as below code 不用使用倒数计时器,您可以简单地使用延迟发布的处理程序和线程。在方法的末尾以指定的时间间隔发布处理程序,如下代码

Runnable myThread = new Runnable() {
    @Override
    public void run() {
        //call the method here
        myHandler.postDelayed(myThread, 1000);//calls thread after 60 seconds
    }
};
myHandler.post(myThread);//calls the thread for the first time

After thinking on this for a while I came up with a solution that satisfies my chief requirement of simplicity. 在考虑了一段时间之后,我想出了一个满足我对简单性的主要要求的解决方案。 Here's how to employ it: 使用方法如下:

Declare two class-level variables 声明两个类级别的变量

private long startTime = 60000; // Set this equal to the length of the CountDownTimer
private long interval = 10000; // This will make the method run every 10 seconds  

Declare a method to run on an interval inside the CountDownTimer 声明一种在CountDownTimer内部的间隔上运行的方法

private void runOnInterval(long millisUntilFinished) {
    if (millisUntilFinished < startTime) {
        playSound();
        startTime -= interval;
    }
}

And then call the method in the onTick method of the CountDownTimer 然后在CountDownTimeronTick方法中调用该方法

// ...
@Override
    public void onTick(long millisUntilFinished) {
        runOnInterval(millisUntilFinished);
    }
// ...

And here's how it works: The onTick method of the CountDownTimer passes millisUntilFinished each time it ticks. 这是它的工作方式: CountDownTimeronTick方法每次计时都会传递millisUntilFinished Then runOnInterval() checks to see if that value is less than startTime . 然后, runOnInterval()检查该值是否小于startTime If it is, it runs the code inside the if statement (in my case, playSound() ) and then decrements startTime by the value of interval . 如果是这样,它将在if语句(在我的情况下为playSound() )中运行代码,然后将interval的值减少startTime Once millisUntilFinished is lower than startTime again, the process starts all over again. 一旦millisUntilFinished小于startTime ,该过程将重新开始。

The above code is simpler than employing another CountDownTimer or a Handler and a Runnable . 上面的代码比采用另一个CountDownTimerHandlerRunnable更简单。 It also automatically works with any sort of functionality one might have added to the activity to handle CountDownTimer pauses and resets. 它还可以自动与可能已添加到活动中以处理CountDownTimer暂停和重置的任何功能结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM