0

時間をもう一度開始 (または更新) する直接的な方法はないことを読みました。私はやろうとします:

Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000, importantMessage, showTimerMessage);
                Const.counter.start();

タイマーをもう一度開始したいたびに、機能していません(古いタイマーが残っています)。私に何ができる?

編集:私もこれを試します

if(Const.counter != null){
                    Const.counter.cancel();
                    Const.counter.start();
                }
                else{
                    Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000, importantMessage, showTimerMessage);
                    Const.counter.start();
                }

しかし、それは一時停止と再開であり、再起動ではありません

EDIT 2: 多分私は自分のコードをもっと書くでしょう:

package com.example.fishe;

import android.os.CountDownTimer;
//countdowntimer is an abstract class, so extend it and fill in methods

public class CustomTimerTask extends CountDownTimer{

    private Messages gameoverMes;
    private Messages showRemailTimeMes;

    public CustomTimerTask(long millisInFuture, long countDownInterval , Messages gameoverMes , Messages showRemailTimeMes) {
        super(millisInFuture, countDownInterval);
        this.gameoverMes = gameoverMes;
        this.showRemailTimeMes =showRemailTimeMes;
    }

    @Override
    public void onFinish() {
        gameoverMes.setMessage("GAME OVER");
        showRemailTimeMes.setMessage(" " + 0);
        Game.gameOver();
    }

    @Override
    public void onTick(long millisUntilFinished) {
        Const.secUntilFinished = millisUntilFinished/1000;
        if( Const.secUntilFinished == 6)
            GameSounds.play(Const.TIME_GOING_TO_OVER,false);
        showRemailTimeMes.setMessage(" " + Const.secUntilFinished);
    }
}

もう一度カウントを開始したにもかかわらず、定義した最初のカウンターが終了した後、常に showRemailTimeMes に「0」が表示されます。

@Override
    public void onResume()
    {

        super.onResume();



        if(Const.counter != null){
            Const.counter = null;
        }
        Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000,
                importantMessage, showTimerMessage);
        Const.counter.start();
}
4

1 に答える 1

1

できることは、CountDownTimer の前のインスタンスを破棄し、次のように新しいインスタンスを再初期化することです。

if(Const.counter != null){
     Const.counter = null;
  }
 Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000,
                                             importantMessage, showTimerMessage);
 Const.counter.start();
于 2013-11-12T12:25:34.507 に答える