I need a regular method in my app. The problem is: the following timer in the开发者_开发知识库 code does its action just once. And the timer does it only(the one time) if I do an action(touch the display) after the time(2000) is over. Do you know what I mean? Maybe it has sth. to do with the "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();
}
}
shcedule(task, 0, 2000)
- use the 3-argument method. The second argument is the delay, and the third is the period. See Timer#schedule(..)
The timer is doing what he should do. You should use the schedule Method of the Timer but instead scheduleAtFixedRate().
精彩评论