I am making an application where on clicking a button, the selected song list is to be downloaded and I need to begin downloading a file only when the file before it has completed downloading. Is there any way to confirm?
Asked
Active
Viewed 152 times
1
-
1You should use transactions and async communication. In other way I could use simple queue with synchronized threads – Marcin Erbel May 04 '15 at 14:27
-
Could you give me an example? Or a link? Would be helpful. – Viren Chugh May 04 '15 at 14:30
-
1I think that Pshemo wrote a good answer, you can also look there: http://stackoverflow.com/questions/8323955/download-files-in-queue-in-android – Marcin Erbel May 04 '15 at 14:36
1 Answers
1
I am not Android or multi-threading expert so there may be better ways to do this, but from what I remember you could use Executors for this scenario, like
ExecutorService es = Executors.newFixedThreadPool(1);// allow only one task to run,
// place rest of task in queue
es.execute(new Runnable() {
@Override
public void run() {
//download file one
}
});
es.execute(new Runnable() {
@Override
public void run() {
//download file two
}
});
Downloading file in second task will be executed only if code in previous task will finish.

Pshemo
- 122,468
- 25
- 185
- 269
-
Would it work if I out a loop around it? Plus, I'll be opening a downloadable link. Hence the task is just to open the link via a browser or default set download manager. – Viren Chugh May 04 '15 at 14:43
-
@VirenChugh I don't see a reason why this wouldn't work, but as I said, I am not an Android expert. You will need to try it. Or maybe first take a look at link provided by @ MarcinErbel in comment under your question. That answer seems to be reasonable. – Pshemo May 04 '15 at 14:46
-
Tried it. I get the concept as well. Opens both links at one time. This is because my function does not download the file but just opens a link which triggers the downloading. Hence once that's done, it moves on to the next file. But this actually helps me out in another project, so thank you. :) – Viren Chugh May 04 '15 at 15:39