How to completely kill/remove/delete/stop an AsyncTask

AsyncTask does not cancel process on

myAsynTask.cancel(true) 

For that you have to stop it manually.

for example you are downloading video in doInBackground(..) in while/for loop.

protected Long doInBackground(URL... urls) {

         for (int i = 0; i < count; i++) {
          // you need to break your loop on particular condition here

             if(isCancelled())
                  break;             
         }
         return totalSize;
     }

When you start a separate thread(AyncTask) it has to finish. You have to manually add a cancel statement to your code inside the AsyncTask.

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

Checkout more in the documentation: http://developer.android.com/reference/android/os/AsyncTask.html


Declare in your class

DownloadFileAsync downloadFile = new DownloadFileAsync();

then On Create

DownloadFileAsync downloadFile = new DownloadFileAsync();
downloadFile.execute(url);

in Your Background ()

if (isCancelled())
    break;

@Override
protected void onCancelled(){

}

and you can kill your AsyncTask by

downloadFile.cancel(true);