I have an application where a user can backup/restore his files to/from a server.
During the restore operation, before downloading each file, I check if the file already exists (assuming I know the original path).
boolean fileExists(String path);
If the file exists, I want to ask the user if he wishes to download and rename, download and replace, or skip the current file.
This could be a custom AlertDialog for example (using AlertDialog.Builder and an xml layout).
So the code would be something similar to:
static final int DECISION_NO_ANSWER = -1;
static final int DECISION_RENAME = 0;
static final int DECISION_REPLACE = 1;
static final int DECISION_SKIP = 2;
int userDecision;
for (RestoreFile file : ListOfFiles) {
userDecision = DECISION_NO_ANSWER;
if (fileExists(file.getOriginalPath())){
OptionsDialog.show();
// wait for answer
}
switch(userDecision){
// handle different cases and update ProgressBar
}
}
Of course this code should be running in a separate thread.
If I use an AsyncTask here’s how I would wait for the answer:
while(userDecision == DECISION_NO_ANSWER){
try {
Thread.sleep(30);
} catch (InterruptedException e) {}
}
If I use a Thread I could use wait() and notify(). I would have to use a Handler to update the ProgressBar.
So which is better (AsyncTask or Thread) ? or is there a third better solution?
An asynctask is run in a thread, like everything. you can use wait and notify, with a handler or the onProgressUpdate to notify the progressBar and everything.