3

Looking at the source code for the stock Calendar app, Google uses an IntentService to do the content provider operations for the database. Is there an advantage to using an IntentService rather than a AsyncQueryHandler?

I thought that since a Handler is tied to the UI, if the activity gets paused or stopped, the Handler would also get paused. However, this doesn't seem to be the case: I created a simple content provider and AsyncQueryHandler that do nothing but run through a long for loop. When I launch other apps or kill the activity, the for loop still runs.

So is the advantage of using an IntentService for asynchronous CRUD operations that the service (being a service) has less of a chance of getting killed?

Update: Part of my confusion is how a handler relates to an activity's lifecycle. From my experiment, it seems that it's independent.

Also, for those not familiar with the stock Calendar app source code, the way it works is that to do a CRUD, it adds the operation to a queue along with a reference to a handler. Then it starts the intent service which pops the queue and does the CRUD. When it's finished, it invokes the handler via Message.sendToTarget().

So what does that extra complexity buy us?

4

1 回答 1

0

我陷入了同样的困惑。我发现 IntentService 将被该活动杀死。这也意味着它将在方向更改时被杀死。虽然 AsyncQueryHandler 将继续运行(如您所见)。在我的用例中,我首先更新我的 UI,而不是等待数据库更新。所以我想我会选择 AsyncQueryHandler。

我对 Intent Service 了解不多(从未使用过),但因为它正在发送消息,当您需要数据库中的某些内容在 UI 线程上显示时,它可能很有用..这样您就知道数据库何时更新。

于 2013-03-06T10:35:08.393 回答