Simple application and precautions of Android TimerTask

Simple application and precautions of Android TimerTask

Timers are often used in Android application development, and TimerTask timer task is inevitable

The following simple example demonstrates how to use TimerTask

This example demonstrates that if there is no touch screen event in 3 seconds, the screen will be locked (just set the text below, meaning it). If there is a touch screen event, the screen will be unlocked

Two issues need to be noted here:

Before each scheduled task, ensure that the previous task has been removed from the timer queue

Create a new object every time you place a task, otherwise the following error occurs:

ERROR/AndroidRuntime(11761): Java.lang.IllegalStateException: TimerTask is scheduled already

Therefore, the same timer task can only be placed once

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>