The timer does its work only once

I need a regular method in my application. The problem is this: the next timer in the code performs its action only once. And the timer does this only (once) if I take an action (tap the display) after a lapse of time (2000). Do you know what I mean? Maybe it is. to do with "OnTouchListener"?

public class DrawView extends View implements OnTouchListener {

    Timer mTimer = new Timer();

    public DrawView(Context context) {
       mTimer.schedule(new Task(this), 2000);
    }

    public boolean onTouch(View view, MotionEvent event) {
    }
}

class Task extends TimerTask {

  private DrawView mDw;

  public Task(DrawView dw){
      mDw = dw;
  }

  public void run() {
        mDw.newgame();
  }
} 
+3
source share
2 answers

shcedule(task, 0, 2000)- use a method with three arguments. The second argument is the delay, and the third is the period. Cm.Timer#schedule(..)

+2
source
0

Source: https://habr.com/ru/post/1758763/


All Articles