What is the point of setting setInterruptIfStopped to true in a JobIntentService?

254 views Asked by At

JobIntentService has the setInterruptIfStopped method which, when set to true, causes the currently running AsyncTask to cancel if the job is stopped:

void setInterruptIfStopped (boolean interruptIfStopped)

Control whether code executing in onHandleWork(Intent) will be interrupted if the job is stopped. By default this is false. If called and set to true, any time onStopCurrentWork() is called, the class will first call AsyncTask.cancel(true) to interrupt the running task.

https://developer.android.com/reference/android/support/v4/app/JobIntentService

However, this doesn't actually interrupt the work running in onHandleWork. Instead, this just sets a boolean in the AsyncTask to true, which I would need to check for in onHandleWork to interrupt my work manually.

public final boolean isCancelled ()

Returns true if this task was cancelled before it completed normally. If you are calling cancel(boolean) on the task, the value returned by this method should be checked periodically from doInBackground(Object[]) to end the task as soon as possible.

https://developer.android.com/reference/android/os/AsyncTask

But for this we already have the JobIntentService's isStopped method:

boolean isStopped ()

Returns true if onStopCurrentWork() has been called. You can use this, while executing your work, to see if it should be stopped.

From my understanding, we can't even access the AsyncTask's isCancelled method. So what is the use of setInterruptIfStopped in JobIntentService?

0

There are 0 answers