Android often encounters tasks that perform periodically and regularly. Beginners often use the Thread.Sleep () method. In Android, there is a timer that can be dedicated to doing this.
Let's see what's in Timer.class. You should know when you see the first few lines of code .... There is a static inner class in the timer: Timerimpltimerimpl has a static inner class: Timerheaptimerimpe inherits the thread, which is used to implement the specific task execution content. Timerheap defines an array of timertask that is used to manage allocations for multiple timertask. The advantage of using static inner classes is that multiple timer objects can be pooled. Here's a look at timertask.class. Well, more simple than a timer, is a runnable implementation. Just a few more objects Lock: Object lock, when the timer in the TimerTask operation, ensure synchronization, thread safety. Cancelled: Flag variable, whether the current timertask has been canceled. When:timer when executing timertask, how much time is delayed. Period:timer the cycle time when the timertask is executed. Fixedrate: Whether a fixed frequency is executed, such as a timertask, the execution period is 3 seconds, maybe a task is not completed in 3 seconds, Fixedtate is true, then the next task will still be executed. If it is Fasle, the next task waits until the previous execution is complete and continues the cycle execution. The way to use it is simple:
The last line of code: Timer.schedule (task,1000,3000) means that a task is executed with a period of 3 seconds after 1 seconds.
Android Timer TimerTask Usage notes