2

In this question: Android: AsyncTask vs Service, someone answered

Service is a daemon, AsynkTask is background thread

and someone replied to that answer with:

A service is not a daemon as it doesn't have its own thread of execution.

QUESTION: Since both operate with threads and work in the background, other than the lifespan of each, what are the key differences between Asynctasks and Daemon processes?

Community
  • 1
  • 1

2 Answers2

2

According to : http://www.linux.org/threads/android-services-and-daemons.7143/

Service:

A "Service" is special software that runs in the background like a daemon on GNU/Linux. Services doesn't have GUIs. A "started service" runs in the background until it completes its task, crashes, or is explicitly closed by the user or application. A "bound service" persists until no more apps are attached or bound to the service.

AsyncTasks:

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.

Daemon:

A "daemon" is a process that runs in the background *without owning a GUI. Services are usually daemons, and daemons are typically considered services. However, the exact difference between services and daemons is blurred. In general, they can be considered the same entity. However, some people use “daemon” to refer to a piece of software and “service” to refer to the actions and APIs offered by a daemon.

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
2

I have not met anywhere term Daemon together with Services or AsyncTask. To me Daemon threads are the ones from java which allow JVM to finish even with unfinished Threads. In terms of Android you have no control over your App process - you at most might kill it.

Android service is a Component - this places it near to Activity component. What does it mean? It is managed by system - it has its lifetime during which life cycle methods are being called. Service can be configured to be recreated, or you can make it a foreground (this way system will be less likely to kill it). Since it is a Component you can configure it to be run under separate process. It does not have its own thread of execution by default - it runs on UI thread.

Now AsyncTask is entirely different thing, it is not a Component, so you cannot do anything from above. One thing it has is a background worker thread. Actually AsyncTask is a wrapper around Exeuctors thread pool with some lifecycle methods - like onPreExecute,onPostExecute,... It should not be compared to services, but rather to Loaders.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • So lets say I start an app upon startup using "ACTION_BOOT_COMPLETED" in the AndroidManifest.xml and I want the app to continue regardless in the background, and to start certain tasks once a specific parameter is met. Should I use a daemon or something else? – Shiraz Chokshi Jan 17 '16 at 22:19
  • @ShirazChokshi - i think `Services` are more comman than that `daemon` on Android. – ʍѳђઽ૯ท Jan 17 '16 at 22:27
  • @LinX64 why should I use a Service in this case? – Shiraz Chokshi Jan 17 '16 at 22:28
  • You shouldn't, that was a suggest.but those are like the same with a little differences between eachother.and you can find a lot of tutorials or examples for doing that and let's consider that, services doesn't have GUIs.i said that because i saw many users are working with that and you should choose which one is the best for your porpuse btw. – ʍѳђઽ૯ท Jan 17 '16 at 22:34
  • 1
    @ShirazChokshi yes, for such long background tasks use Service - there is no way to use AsyncTask in your scenerio. You can execute AsyncTask from inside of Service if you like. If your background task must execute for long time then you can make your Service foreground - but that requires you to setup a Notification - you might think about it as a kind of GUI side of Service. Service will report its work using this Notification - like ongoing file download - progress. – marcinj Jan 17 '16 at 22:37
  • @luskan ahh this makes sense! Thank you so much! – Shiraz Chokshi Jan 17 '16 at 22:41