在onPause内部,我在名为PlayTask的AsyncTask上调用cancel(true)。但是,当我点击另一个活动时,它仍然欢快地继续。为什么我的取消电话不被尊重?
仅供参考,这是一系列事件:
CreateTrackActivity: btnPlay clicked
CreateTrackActivity: getting player for track
BasePlayerActivity : playing trac
BasePlayerActivity : Track playing
BasePlayerActivity : onPAUSE
BasePlayerActivity : play task cancelled
BaseActivity: onCreate
BasePlayerActivity: track received
BasePlayerActivity: track prepared
BasePlayerActivity: track setup finished
答案 0 :(得分:2)
当您尝试执行asyncTask.cancel(true)时,有一些意外的原因。
所以,cancel(true)并不一定会从doInBackground()中停止执行。如果cancel(true)返回true,则表示任务不会运行onPostExecute()。
所以,你必须在doInBackground()中定期检查isCancelled()。