I've an AsyncTask that I shut down in the Activity's onPause lifecycle event, so it doesn't run when someone leaves the app, but it keeps going despite this. I added some tracing and this snippet shows the problem.
Trace.d(TAG,"Task state: " + myTask.getStatus() );
myTask.cancel(true);
Trace.d(TAG,"Task state: " + myTask.getStatus() );
Outputs:
Task state: RUNNING
Task state: RUNNING
Why is the cancel() method not having any effect on the state of the task? I notice the docs say the cancel method will "attempt" to stop the task, but under what circumstances will it fail? The task is definitely running as it is outputting log output every ten seconds, and as you can see above its status is returned as running.
Update: I added tracing to show me the isCancelled() state as well and that DOES change. So the call to cancel(true) is changing the cancelled state from false to true, but apparently having no effect on the Status, or stopping the thread.