How to stop handler inside an asynctask

0

I have the following asynctask, which contains a handler inside it, but when I exit the activity I close the async but sometimes the handler continues to call the async after it is closed. It does not crash in the application but it keeps sending logs as if it were giving error, it follows async below

public class PostImageAsyncTask extends AsyncTask<Object, Object, PutObjectResult> {

    public PostImageAsyncTask() {
        super();

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // before making http calls

    }

    @Override
    protected PutObjectResult doInBackground(Object... params) {
        try {
            return amazonS3Client.putObject(por);
        } catch (AmazonServiceException e) {
            return null;
        } catch (AmazonClientException e) {
            return null;
        }

    }

    @Override
    protected void onPostExecute(PutObjectResult result) {
        if (!isCancelled()) {
            if (result != null) {
                showAndHideLoad(false);
                if(step == 0) {
                    picture = new Picture(amazonUrl, null);
                } else if(step == 1) {
                    documentIDs = new DocumentIDs(amazonUrl,null);
                } else if( step == 2){
                    documentIDs.setBackURL(amazonUrl);
                }

                if ((snackbar != null && snackbar.isShown())) {
                    snackbar.dismiss();
                }
                send_continue();
            } else {

                if (RECONECT_TIME_DELAY < MAX_TIME_DELAY) {
                    if ((snackbar == null || !snackbar.isShown())) {
                        isClicked=false;
                        snackbar = Snackbar.make(coordinatorLayoutView, getString(R.string.snackbar_internet_fail), Snackbar.LENGTH_INDEFINITE);
                        View sbView = snackbar.getView();
                        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                        textView.setTextColor(Color.WHITE);
                        snackbar.setActionTextColor(Color.RED);
                        snackbar.setAction("Cancelar", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //
                            }
                        });
                        snackbar.show();
                    }
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            RECONECT_TIME_DELAY = RECONECT_TIME_DELAY * 2;
                            postImageAsyncTask.cancel(true);
                            postImageAsyncTask = new PostImageAsyncTask();
                            postImageAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }
                    }, RECONECT_TIME_DELAY);
                }
            }
        } else {
            postImageAsyncTask.cancel(true);
        }
    }
}
    
asked by anonymous 04.02.2016 / 22:15

1 answer

2

AsyncTasks were not made to match Handler . You can do what you want (update screen elements every time an error occurs while connecting to Amazon) only with AsyncTask , without needing a Handler to do so.

AsyncTasks allow you to update the screen in the middle of the execution of an asynchronous task by calling the publishProgress() method.

In the doInBackground() method loop that tries to connect to Amazon a certain number of times. Each time the connection fails, call the publishProgress() method. This will result in the AsyncTask.onProgressUpdate() method being called, and within it you can refresh the screen as desired.

Leave the onPostExecute() method to the situation where the connection was successful.

    
05.02.2016 / 23:23