Android: Asynctask VS Service

Android: AsyncTask vs Service

In some cases it is possible to accomplish the same task with either an AsyncTask or a Service however usually one is better suited to a task than the other.

AsyncTasks are designed for once-off time-consuming tasks that cannot be run of the UI thread. A common example is fetching/processing data when a button is pressed.

Services are designed to be continually running in the background. In the example above of fetching data when a button is pressed, you could start a service, let it fetch the data, and then stop it, but this is inefficient. It is far faster to use an AsyncTask that will run once, return the data, and be done.

If you need to be continually doing something in the background, though, a Service is your best bet. Examples of this include playing music, continually checking for new data, etc.

Also, as Sherif already said, services do not necessarily run off of the UI thread.

For the most part, Services are for when you want to run code even when your application's Activity isn't open. AsyncTasks are designed to make executing code off of the UI thread incredibly simple.

Difference between Service, Async Task & Thread?

Probably you already read the documentation description about them, I won't repeat them, instead I will try to give answer with my own words, hope they will help you.

  • Service is like an Activity but has no user interface. Probably if you want to fetch the weather for example you won't create a blank activity for it, for this you will use a Service.

  • A Thread is a Thread, probably you already know it from other part. You need to know that you cannot update UI from a Thread. You need to use a Handler for this, but read further.

  • An AsyncTask is an intelligent Thread that is advised to be used. Intelligent as it can help with it's methods, and there are three methods that run on UI thread, which is good to update UI components.

I am using Services, AsyncTasks frequently. Thread less, or not at all, as I can do almost everything with AsyncTask.

Asynctask vs Thread vs Services vs Loader

AysncTasks are not 'outdated' as much as they are incomplete.
Among other things async tasks do not bother if their parent activity is currently running or not. For the same reason why you include checks for verify the context to be null or not. Besides, unless you are using your own Thread Pool Executor these tasks execute serially.

Volley tries to fill in those gaps, mainly concerning syncing up with the main thread and thread pooling. It behaves optimal if you wish to do stuff that requires average network requests; like some meta data list and images(picture the youtube app requests and facebook app requests for posts).

Typically few advantages of Volley are as follows

  1. It keeps the worker thread informed about the activity(Main thread)
  2. Easier resource prioritization you could provide priority to your download requests.
    A typical scenario would involve you giving priority to text over image.
  3. Effective request cache and memory management.
  4. Extensible
  5. It provides you an option to discard your request in-case your activity was shutdown or restarted.
  6. Simpler patterns for data retrieval as opposed to AsyncTasks.

Volley fares badly when it comes to streaming requests/video as mentioned at Google I/O.

I'm not exactly aware of robospice.
Ps: If you have time on your hand see https://www.youtube.com/watch?v=yhv8l9F44qo

Here's a further read if you wish to go into other libraries with benchmarks for the same.
Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley

Async task versus service to download data

In any case you should use AsyncTask because even service runs in the main (GUI) thread where no networking should be done. Whether to run the AsyncTask in a service or an activity depends on whether you want that download to continue in background.

IntentService vs AsyncTask for Login Activity?

Using AsyncTask will help to make your code much more simpler in your case.
In this case you will use just one class into another. In this case you transfer the data in the most easiest way.

In the second case - intentservice - you have to write/read/collect/extract your data, time lags and so on.

EDIT
1. You can cancel request before rotation
2. You can check if your request is in progress and do not launch it again.
3. You can disable rotation in the login screen.



Related Topics



Leave a reply



Submit