Do it like this:
public static class OperationFiles extends AsyncTask<Void, Integer,Void>
{
public enum Operation
{
Move,
Rename
}
private ProgressDialog progressDialog;
private Context context;
private OnPostExecuteListener onPostExecuteListener;
private List<File> filesList;
private String destino;
private Operation operation;
public OnPostExecuteListener getOnPostExecuteListener() {
return onPostExecuteListener;
}
public void setOnPostExecuteListener(OnPostExecuteListener onPostExecuteListener) {
this.onPostExecuteListener = onPostExecuteListener;
}
//aqui você pode receber a lista de arquivos e o novo local
public OperationFiles(Context context,List<File> filesList, String destino, Operation operation)
{
this.context = context;
this.progressDialog = new ProgressDialog(context);
this.filesList = filesList;
this.destino = destino;
this.operation = operation;
}
@Override
public void onPreExecute()
{
if(operation == Operation.Move)
this.progressDialog.setMessage("Movendo os arquivos...");
else
this.progressDialog.setMessage("Renomeando os arquivos...");
this.progressDialog.setMax(filesList.size());
this.progressDialog.show();
}
@Override
protected Void doInBackground(Void... voids)
{
for(int i = 0; i < filesList.size(); i++)
{
//faz a operação para defina no construtor aqui
if(operation == Operation.Move)
{
}
else
{
}
//itera no progresso
publishProgress(i);
}
return null;
}
@Override
public void onProgressUpdate(Integer... progress)
{
this.progressDialog.setProgress(progress[0]);
}
@Override
public void onPostExecute(Void result)
{
//dismiss do diálogo
this.progressDialog.dismiss();
//chama o callback informando o término do processo
if(getOnPostExecuteListener() != null)
getOnPostExecuteListener().onPostExecute();
}
//Irá fazer um callback para quando o processo terminar
public interface OnPostExecuteListener{
public void onPostExecute();
}
}
Consider that this is an example and you can change according to your problem.
So I did, as soon as one operation is completed, he calls the other, making two different dialogues run.
The use would look like this:
//Faz a operacao de mover os arquivos
OperationFiles operationFiles = new OperationFiles(this, new ArrayList<File>(), "/sdcard...", OperationFiles.Operation.Move );
//informa o callback
operationFiles.setOnPostExecuteListener(new OperationFiles.OnPostExecuteListener()
{
@Override
public void onPostExecute()
{
///aqui a operacao de renomear
OperationFiles operationFilesRename = new OperationFiles(Main.this, new ArrayList<File>(), "/sdcard...", OperationFiles.Operation.Rename);
operationFilesRename.execute();
}
});
operationFiles.execute();
The ideal would not be a bar Progress showing the steps it performs?
– Reiksiel
I find the idea of @Reiksiel very cool, even thinking about UX. I think that after the first dialog the user does not expect another hehe. Try to use one
DialogFragment
, in it you can customize theView
and put aTextView
with the stages.– Wakim
I hadn’t thought about it... But there’s no way to change the title and message of the Dialog within the thread? I would need to change them too, tried but just doesn’t change...
– felipe.rce
@Wakim now got good, I was able to do... Give me an answer showing an example of how to customize the view in this part, that I choose the answer
– felipe.rce
@Felipe.rce, you already have an answer with this idea. If it wasn’t as you implemented it, you can write the answer and accept it. Staying as information for the next who come to have this doubt.
– Wakim