How to handle queue of background tasks in Android?
I am new to Android programming and my scenario goes like this.
I have several tasks which are in queue like this:
Name. Time.(sec) and some other attributes Task 1 0 Task 2 1.5 Task 3 3.5 Task 4 6.5 Task 5 9.5 Task 6 10.5 Task 7 11.5 Task 8 12 ...
This table basically says that execute Task 1 at 0th second, Task 2 at 1.5 second, Task 3 at 3.5 second and so on. I am using Task 1 as the starting time i.e. 0th second. It may be possible that there are multiple tasks running in parallel i.e Task i may still be running when it is time to schedule Task j. All of these tasks are network-intensive.
As I said I am not familiar with Android and I'm trying to figure out the best way to tackle this in Android.
I am thinking of using one AsyncTask(let's say Queue) that loops through all the tasks and see if it is time to schedule any task. If it is not time to schedule any task then sleep till next schedule time i.e nextTaskTime - currentTime. If it is time to schedule then create Runnable instance(or may be another AsyncTask) for the Task and start the execution. So, basically Main thread will create AsyncTask named Queue which may in turn create other threads for Tasks.
Is this correct way to approach this problem? If there are other ways then please let me know.
Checkout this link
You can get the sample code from this link
You will the Serial executor it will execute the Background task serially likewise queue...
DownloadImageTask o1 = new DownloadImageTask(); o1.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);