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.

Thank you.

Answers


Checkout this link

http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/.

You can get the sample code from this link

http://mindtherobot.com/blog/wp-content/uploads/2010/06/downloadqueue.zip


You will the Serial executor it will execute the Background task serially likewise queue...

DownloadImageTask o1 = new DownloadImageTask();             
o1.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);

Need Your Help

IE slows down animated GIFs with more than 6-8 FPS. Is there a fix or a cross-browser alternative (except flash)?

javascript internet-explorer animation gif animated-gif

Honestly, until now I never noticed, but IE actually slows animated GIFs with more than 6-8 FPS (12-16ms/frame) down. For example, open this GIF in IE and in Chrome - you will see the difference:

Ionic how to fire a function just before app is closed?

angularjs ionic-framework

I'm using ionic and I would like to run a function to clear my localstorage once a user triggers an event to close the app.