I can not get the FINISHED status of an AsyncTask on Android

1

I am running AsyncTask in the following way:

MyAsyncUploadFilesTask myAsyncUploadFilesTaskFF = new MyAsyncUploadFilesTask();
Log.e(TAG, myAsyncUploadFilesTaskFF.getStatus().name()); // PENDING

for (Report rp : reports) {
    myAsyncUploadFilesTaskFF.execute(SERVER_URL_TO_UPLOAD_PHOTOS, rp.getFotoFrontal1());
    Log.e(TAG, myAsyncUploadFilesTaskFF.getStatus().name()); // RUNNING
}

Log.e(TAG, myAsyncUploadFilesTaskFF.getStatus().name()); // RUNNING cuando debería ser FINISHED

My problem is that I did not manage to get the status of FINISHED , how can I correct my code to be able to know when my AsyncTask has finished?

    
asked by Jorius 15.12.2016 в 17:07
source

2 answers

2

So you can identify the states of your task

if(myAsyncUploadFilesTaskFF.getStatus() == AsyncTask.Status.PENDING){

}

if(myAsyncUploadFilesTaskFF.getStatus() == AsyncTask.Status.RUNNING){

}

if(myAsyncUploadFilesTaskFF.getStatus() == AsyncTask.Status.FINISHED){

}
  • PENDING: Your homework has not started
  • RUNNING: doInBackground is running
  • FINISHED: I finish

As you are printing it takes myAsyncUploadFilesTaskFF.getStatus().toString()

I imagine you could do something like this (Source of the answer of stack over flow in English, but try to explain a little about Handler )

Handler myHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                //Termino de ejecutar la tarea
                break;
            default:
                break;
        }
    }
};

And within the method onPostExecute (which already ended)

@Override
protected void onPostExecute(Boolean result) {
    myHandler.sendEmptyMessage(0);
}

According to the documentation of the Handler object, there are two utilities for:

  

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

Spanish (sorry if the translation is not very good):

  

There are two main uses for a Handler (1) To program messages that will be executed at some future time * and (2) To queue an action to be performed on a Thread different from the current one

In summary the use number (2) is in your case, since you are in your main thread, you want to identify when another thread ends other than the main one (your asynchronous task) and execute an action (Print something)

    
answered by 15.12.2016 / 17:12
source
1

Instead of printing the status:

Log.e(TAG, myAsyncUploadFilesTaskFF.getStatus().name()); // RUNNING cuando debería ser FINISHED

get the Status and compare it with the constant that defines that I finish Asynctask :

if(myAsyncUploadFilesTaskFF.getStatus() == AsyncTask.Status.FINISHED){
      Log.e(TAG, "Finalizo!");
}

If the status does not indicate that the Asynctask has ended, what happens is that the execution of the code is done sequentially but the Asynctask process is performed asynchronously, for that reason when it reaches this point it does not say FINISHED because it's not really over.

    
answered by 15.12.2016 в 18:10