user5900017 February 2016

The purpose of a service

From what I understood, an Android service and is nothing more than a wrapper around, respectively manager for, a thread. One can't do any lengthy operations as it is running in the context of the application's main thread (which happens to manage UI calls as well).

Could someone shed some light on the purpose of a service, if the Application class would basically cover the same functionality (http://stackoverflow.com/a/31490935)

Answers


Damanpreet Singh February 2016

By default service runs in main thread but you can run it in different thread as well. Also it has its own lifecycle and it can run in both started state as well as bound state. Hence service is useful to do long running tasks if it runs in a background thread.

Service is not a wrapper around thread. It is having a proper lifecycle methods.


Bryan February 2016

From my understanding, the benefits of an Android Service are that you can run a thread that is separate from the Application as a whole. This allows you to continue operations of the Service even if the Application is not running.


CommonsWare February 2016

an Android service and is nothing more than a wrapper around, respectively manager for, a thread.

No. Services do not have a separate thread by default. Services can create threads (and usually do), and some specific Service subclass create threads by default (e.g., IntentService).

the Application class would basically cover the same functionality

The Application class does not cover the same functionality, any more than an AnonymousCoward class would. An Application instance does not create a thread, either. Any sort of thread manager (e.g., ThreadPoolExecutor) might be used by a service but does not replace a service.

Quoting myself:

Primarily, a service exists as a flag to the operating system, indicating that you are actively delivering value to the user, and therefore Android should aim to terminate other processes — not yours — when it gets low on system RAM. Secondarily, a service provides a couple of APIs for IPC: the “command” pattern (Intent and startService()) and the “binding” pattern (bindService()).

An Application always exists in an app (an instance is created when the process is started). It does not serve as any sort of flag to the OS to indicate process importance, nor does it have any sort of IPC capability.

Post Status

Asked in February 2016
Viewed 2,036 times
Voted 7
Answered 3 times

Search




Leave an answer