Asynctask finish running

Asked

Viewed 647 times

1

I wonder how I could pause, cancel this function as soon as I close the Activity that is running.

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}
  • 1

    Asynctask you can cancel at any time, just call the Cancel(Boolean) method. It is very important to understand that the task is not canceled immediately and ideally you should always check inside the execution if the task was canceled using the method: isCancelled() As you comment about pausing or canceling when leaving Activity the ideal is to treat inside the onPause() method if you want to stop once you lose focus.

1 answer

1

I confess with some shame that I have never tried this in my applications, although it is a feature that can be considered basic (interrupt an HTTP request), but I believe the following code will work when called task.cancel():

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    private ImageView bmImage;
    private InputStream in = null;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            HttpURLConnection conexao = new java.net.URL(urldisplay).openConnection();
            new Thread(new Runnable() {
                @Override
                public void run() {
                   while (in == null && false == isCancelled()) {
                       // Aguarda conexão ser estabelecida e InputStream estar disponível
                   }

                   if (isCancelled()) {
                       conexao.disconnect();
                   }
                }
            }).start();
            in = conexao.getInputStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

The solution is not yet complete because the reading of the InputStream in the event of a call from task.cancel() (although it is possible that it will end up working anyway in case the disconnect() take the InputStream being read to launch a IOException), but I don’t have time right now to think of a solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.