- import java.util.*;
- Timer timer = new Timer();
- create subclass of TimerTask, eg.: class MyTask extends TimerTask { ... }
- add "public void run()" method in subclass
- timer.schedule(new MyTask(), 0, 30); // assume no delay and 30 millisec interval
- timer.cancel(); // for cleanup. to start timer again, need to instantiate: new Timer(); then schedule(...)
- if using "protected void onPause()" or onResume(), ensure that super.onPause(), etc is called.
- if need to refresh View UI, then use postInvalidate() instead of invalidate(). else will have exception
Activity life cycle (to understand when to stop Timer to prevent waste of resources):
No comments:
Post a Comment