androidandroid-serviceandroid-service-bindingandroid-components

Why Android Service was made as an another application components?


As for the other application components like activities,Intent, Broadcast Receiver, i can understand their use case but whatever use case of Service is written in the documentation can be achieved using simple Java class having static members (variables and methods) . Like if i need a download file service . I can have a static method which will receive taskListener and other required parameters (eg. url of file) in the argument and download the file async and give the callback to taskListener's callback method. So why should i use service for that.

Please someone explain me the reason of using service with example


Solution

  • First, a service can be started or bound to from outside the application, if you wish, by having the service be exported. That cannot be done via a simple Java class. This is essential for most of the specialized services (input methods, AccessibilityService, TileService, etc.).

    Second, a service raises the importance of a process, telling Android that it is doing work on behalf of a user. This helps keep the process around for longer, before Android terminates that process to free up system RAM for other apps. That cannot be done via a simple Java class. So, for example, while you can download a file using a simple Java class, Android might terminate your process before that download can be completed. This is less likely if the download is managed by a service.