Cancel file upload using httpclient and asynctask

In my application, I need to download files from URL locations. I want to show the download progress in a dialog box (or, optionally, in the notification area). I came across several good resources on this subject (something like http://progrnotes.blogspot.com/2010/09/c-android.html ). Unfortunately, all the examples do not give a clear indication of how to correctly cancel the download at the user's request. So my question is actually quite simple:

Given an asynctask that downloads a file in the background (using httpclient) and displays a download dialog and a cancel button, how do I cancel the download and stop the background task when the button is clicked?

I know that killing threads are generally not a good idea, so I will probably have to work with the "cancel" variable in my background thread. How to transfer a stop signal from a button to asynthesis?

Regards, Ivo

+6
android android-asynctask
source share
3 answers

Name the button AsyncTask.cancel(true) and press isCancelled() inside doInBackground(Params... params) . This way you can tell the background thread that the download should be canceled, and you can take the appropriate steps to stop it.

+3
source

I would call cancel (true) in your AsyncTask object. This will interrupt your thread with normal interrupt handling . Then you can ask AsyncTask if it isCancelled () .

+3
source

I suggest you go through this link for the dark side of AsyncTasks : http://bon-app-etit.blogspot.in/2013/04/the-dark-side-of-asynctask.html .

Google has released a library called "Volley", which is currently used for faster and more efficient networking.

It solves bad points for AsyncTasks.

Cancel request using volley

0
source

All Articles