Alex Haycock Alex Haycock - 1 year ago 76
Android Question

How do you use a TimerTask to run a thread?

I'm struggling to find documentation for the TimerTask function on Android.
I need to run a thread at intervals using a TimerTask but have no idea how to go about this.
Any advice or examples would be greatly appreciated.

Answer Source

You use a Timer, and that automatically creates a new Thread for you when you schedule a TimerTask using any of the schedule-methods.


Timer t = new Timer();
t.schedule(myTimerTask, 1000L);

This creates a Timer running myTimerTask in a Thread belonging to that Timer once every second.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download