0

これが安全なことかどうか、誰か教えてもらえますか?カウントダウン タイマー (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
        }
    }
4

3 に答える 3

0

タイマーを再度初期化する必要はありません....これを試してください...

   int temp=nexttime;
   public class TableCount extends CountDownTimer
   {
       public void onFinish() {
       nexttime=temp;
       timer.start();
       }
   }
于 2012-11-24T10:43:04.750 に答える
0

TableCount の単一宣言の参照を、前のオブジェクトを暗黙的に逆参照する新しいタイマーに変更するだけなので、メモリ リークは発生しません。

実行ごとに新しいタイマーを作成し、それを配列に追加するなどの奇妙なことを行ったとしても (たとえば)、リークは発生しません。最終的にメモリが不足する可能性がありますが、これはリークと同じではありません。これは、アクティビティが終了したときであり、静的参照を別の場所に保持していないと仮定すると、メモリが解放され、ガベージ コレクションの対象となります。

ただし、既存のタイマーを再利用して、schedule() を使用してもう一度実行してみませんか?

于 2012-11-24T10:41:31.210 に答える
-1
public class ServiceCount extends CountDownTimer
{
    public ServiceCount(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish()
    {
             count = new ServiceCount((long) (1 * 60 * 1000), 1000); // 1 minute
             count.start();
    }

    @Override
    public void onTick(long millisUntilFinished)
    {
        Log.d("timer", "" + millisUntilFinished / 1000);
    }
}

ServiceCount count = new ServiceCount((long) (1 * 60 * 1000), 1000); // 1 minute
count.start();
于 2012-11-24T10:45:40.813 に答える