problemo
problemo

Reputation: 629

Android recuring task best option?

Good morning everyone,

I am sending data to a device from android each 40ms. Up until now, I have been using a while(true) thread and thread.sleep because I didn't know better :). Now I see I have a lot of "better" options like:

TimerTask

Asynctask

ScheduledThreadPoolExecutor

Which is the best one for my scenario? Keep in mind that there may be an exception thrown if the device disconnects so I will need to stop sending values until the connection is restored. Furthermore, the data must be sent at pretty precise intervals and it should, in no case, be sent less than 40ms before the previous one.

Thanks!

Upvotes: 0

Views: 70

Answers (1)

vkinra
vkinra

Reputation: 943

Plenty of options, however, just prior to that AsyncTask does not really belong to that list. Asynctask is simply used to perform an operation in a background thread outside of the main UI thread and not really used for scheduling repeating tasks.

For repeating tasks, the options are:

  1. Android: execute code in regular intervals
  2. Use a countdowntimer as the countdowntimer executes in the main thread (if that is what you want)
  3. Or use a TimerTask.

My suggestion for your case is option 1 or 3.

-V

Upvotes: 1

Related Questions