They are both used to offload work from the main thread. All components in Android run on the main thread. the UI runs on the main thread. the broadcast receivers run on the main thread. Android Services run on the main thread. So it is really useful to have some facility that can offload work from the main thread.

In the #AndroidSDK both #AsyncTask and #IntentService can do this work. Are there any guidelines as to which is appropriate when?

satya - 2/1/2014 10:57:03 AM

#IntentService

#IntentService

Search for: #IntentService

IntentService, in addition to offloading work from the main thread is actually an android service. This means it is a full fledged Android Component with a well defined lifecycle. Being a service it can get restarted if stopped in the middle due to low memory conditions etc.

However being a self contained component it usually doesnt hold direct pointers to its callers. For instance if an activity was to invoke it, it will have to report its progress indirectly through a notification manager or other means. It is unlikely that you monitor the progress of an intent service through a dialog but rather use it for a long running work and occasionally peek at it when needed.

This is often used from broadcast receivers. However there is no reason that it cannot be used from other services or activities as well.

satya - 2/1/2014 11:00:27 AM

#AsyncTask

An android asynctask is often used from an activity to offload work from the main thread. It is often monitored through a dialog or a progress bar in a visual context to the user. When used in conjunction with an activity an asynctask can hold pointers to its originating activity but must be aware when the activity is restarted should it happen. An asynctask also given an opportunity to the invoker to stop it quite easily. Also when the process is taken out an asynctask has no lifecycle tied to it so it is gone. An implication of this is, for example if you were to use this from a broadcast receiver, when the broadcast receiver returns the process is taken out although the asynctask is still running.

satya - 2/1/2014 11:08:46 AM

So when do in #Android when do I use AsyncTask and when do I use the #IntentService?

So when do in #Android when do I use AsyncTask and when do I use the #IntentService?

Search for: So when do in #Android when do I use AsyncTask and when do I use the #IntentService?

An activity that can check for such things as emails, or some server side content, or something that takes a little longer and wants to give user a sense of progress can use an AsyncTask lot more easily than using a heavier intent service. However if a user action triggers a long running "state changing" activity an intent service is probably better as it can persist beyond the process life time if needed.

A broadcast receiver is better off using an intent service because an asynctask will fold when the receiver returns.

A service can probably use an asynctask as the service already provides the life cycle for the asynctask to stick around. An asynctask has some monitoring callback methods that may not make a whole lot of sense for a calling service but again I dont see a harm without looking into it deeply.

A service can certainly use an intent service as it does offer to offload the work. But again if that is the case why not the called call the intent service directly. But if there were one to exist and want to reuse it from the context of another service, I can't imagine why you can't use it!!

satya - 2/1/2014 11:13:00 AM

IntentService Vs AsyncTask in AndroidSDK

IntentService Vs AsyncTask in AndroidSDK

Search for: IntentService Vs AsyncTask in AndroidSDK