任何人都可以告诉我,如果这是一件安全的事情吗?我运行一个倒数计时器(CountDownTimer),当计时器达到零时,它必须重新开始计数,例如计算一个较长的时间。要做到这一点,我打电话Android CountDownTimer范围
timer = new TableCount(nextTime * 1000, 100);
在onFinish()方法。
它运行没有问题,但我担心它可能会导致内存泄漏。我是否应该让计时器发出某种通知,告知它已完成?以下是活动代码的重要位:
public class TableActivity extends Activity {
TableCount timer; // the count down timer
protected int nextTime;
...
// somewhere I call this - user clicked the "start" button
timer = new TableCount(nextTime * 1000, 100);
nextTime += 100; // for example
...
public class TableCount extends CountDownTimer
{
public void onFinish() {
... // check if number of iterations has been reached, else:
// start counting down from the next value
timer = new TableCount(nextTime * 1000, 100);
nextTime += 100; // for example
}
}
谢谢 - 我做8次迭代的最大值,所以我认为,内存使用是安全的。顺便说一下,CountDownTimer似乎没有schedule()方法 - 似乎并不是太容易重置countdowntimer – mogoman
对不起,我在想Timer,而不是CountdownTimer。在那种情况下,你在做什么很好。实际上,你甚至没有添加到使用的内存中。我关于循环的一点是,如果你不知道如何保持定时器的前一个实例,那么它只会使用增加的内存,而你并没有这样做。 – Simon