1

I use AsyncTask to download a file from the Internet, but when I call cancel to cancel the AsyncTask, the thread does not stop downloading. And when I do a test, I find when finishing the Activity, Android will not stop the thread in the Activity. So how can I stop downloading the file? Thanks!

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
peter_feng
  • 855
  • 2
  • 7
  • 11
  • How are you calling `AsyncTask.cancel`? Do you pass true for the `mayInterruptIfRunning` parameter? – Dan Breslau Dec 06 '10 at 03:24
  • similar question http://stackoverflow.com/questions/2735102/ideal-way-to-cancel-an-executing-asynctask – Aaron Saunders Dec 06 '10 at 04:00
  • i have called AsyncTask.cancel(true),and the doInBackground() will not return a value,but i have found that the doInBackground() is still running with some log test. – peter_feng Dec 06 '10 at 04:53
  • 1
    Would you mind sharing your solution? Thanks! – Zarah Jan 27 '11 at 04:01
  • i make a flag,when the flag is true continue to download,when calling AsyncTask.cancel,set the flag to false,then the thread will be over – peter_feng Feb 24 '11 at 06:51
  • Possible duplicate of [Android: Cancel Async Task](https://stackoverflow.com/questions/6039158/android-cancel-async-task) – Swati Garg Jul 21 '19 at 08:12

0 Answers0