Abort the running AsyncTask. - Android android.os

Android examples for android.os:AsyncTask

Description

Abort the running AsyncTask.

Demo Code


import android.os.AsyncTask;

public class Main{
    /**//from  w  ww  .j a  va2 s .  c  o  m
     * Abort the running task.
     *
     * @param theTask
     * @return True if the task has been successfully aborted. False otherwise.
     */
    public static boolean abort(AsyncTask<?, ?, ?> theTask) {
        if (!AsyncTaskUtils.isRunning(theTask)) {
            return false;
        }
        theTask.cancel(true);
        theTask = null;
        return true;
    }
    /** Returns whether the task specified as an argument is running or not.
     * @param theTask {@link AsyncTask} to be checked.
     * @return True if the task is running, false otherwise.
     */
    public static boolean isRunning(final AsyncTask<?, ?, ?> theTask) {
        return (theTask != null && theTask.getStatus().equals(
                AsyncTask.Status.RUNNING));
    }
}

Related Tutorials