1

I am writing simple game, where some action must accelerating during the process. The question is how to change timer's period?

    timer = new Timer();
    timerTask = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //
                    // I need to change timer's period here
                    //
                }
            });
        }
    };
    timer.schedule(timerTask, 0, period);

Will be glad to hear any advices.

Reporter
  • 3,897
  • 5
  • 33
  • 47
Red Hot Chili Coder
  • 1,218
  • 1
  • 10
  • 19

1 Answers1

0

I assume that you are performing some logic within the run() method of the TimerTask.

I think a simpler way to go about this would be to use a Handler. This is possibly more idiomatic for Android:

private final Handler mHandler = new Handler();

private final Runnable mTask = new Runnable() {
    @Override
    public void run() {
        // Do your logic.
        // Now post again.
        mHandler.postDelayed(mTask, /* choose a new delay period */);
    }
};    

public void init() {
    delay = 1000L; // 1 second.
    mHandler.postDelayed(mTask, delay);
}
  • I am not sure i understand everything. Will I be able to do something else during those delays? For example to click button. Or it will delay first and then listen to my click? – Red Hot Chili Coder Sep 02 '14 at 13:39