我正在http request使用AsyncTask,我想要的是当我注销时,AsyncTask应该停止。所以我打电话cancel(true)来onStop()。当我调用cancel(true)时,尚未开始的请求被取消,但问题是isCancelled()从未调用它检查正在执行的任务是否被取消。我正在检查isCancelled()Is doInBackgroud()there AsyncTaska way to stop the execution AsyncTask. 以下是场景。
class AsyncClass extends AsyncTask<>{
@Override
protected String doInBackground(Void... params)
{
if(isCancelled())
{
Log.d("isCancelled", iscancelled());
}
//call the webservice
}
}
现在我要打电话的还有其他课程
if(asyncTaskObject!=null){
asyncTaskObject.cancel(true);
asyncTaskObject=null;
}
但是 iscancelled() 中的 Log 语句永远不会被调用。