开发者

order of operations when a Service gets shut down

I'm writing a Service that uses an AsyncTask to do some network operations. Let's say the doInBackground of the AsyncTask is taking a very long time, and while that's happening, resources get low and the operating system is shutting down the Service.

When does the AsyncTask get killed? Will the AsyncTask still be running when onDestroy is called, or will it happen afterwards, or am I responsible for shutting down the AsyncTask? I'm trying to make sure I do not have race conditions between the code in my onDestroy and the code in my doInBackgroun开发者_如何学Cd.


The AsyncTask will continue running until either you cancel it or the system destroys the process hosting it. The system doesn't know, per se, about your AsyncTask so its not going to do anything about it when shutting down the Service, it certainly doesn't know the Service created the AsyncTask.

Its very likely that after onDestroy is called that the application's process will be destroyed as well and so will the AsyncTask. However, the safest policy is to be proactive and cancel the AsyncTask in onDestroy.

A perhaps interesting side note is that requests queued for processing will be lost if the process dies before they are processed. As such, any operations sent to an AsyncTask that must be completed need to be written to a persistent task log so they can be reissued if not completed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜