android - Ideal way to cancel an executing AsyncTask -


i running remote audio-file-fetching , audio file playback operations in background thread using asynctask. cancellable progress bar shown time fetch operation runs.

i want cancel/abort asynctask run when user cancels (decides against) operation. ideal way handle such case?

just discovered alertdialogs's boolean cancel(...); i've been using everywhere nothing. great.
so...

public class mytask extends asynctask<void, void, void> {      private volatile boolean running = true;     private final progressdialog progressdialog;      public mytask(context ctx) {         progressdialog = gimmeone(ctx);          progressdialog.setcancelable(true);         progressdialog.setoncancellistener(new oncancellistener() {             @override             public void oncancel(dialoginterface dialog) {                 // set running = false; right here, i'll                 // stick contract.                 cancel(true);             }         });      }      @override     protected void onpreexecute() {         progressdialog.show();     }      @override     protected void oncancelled() {         running = false;     }      @override     protected void doinbackground(void... params) {          while (running) {             // hard work         }         return null;     }      // ...  } 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -