Differencebetween an Intentservice and a Service

What is the difference between an IntentService and a Service?

In short, a Service is a broader implementation for the developer to set up background operations, while an IntentService is useful for "fire and forget" operations, taking care of background Thread creation and cleanup.

From the docs:

Service
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

IntentService
Service is a base class for IntentService Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

Refer this doc - http://developer.android.com/reference/android/app/IntentService.html

Service vs IntentService in the Android platform

Tejas Lagvankar wrote a nice post about this subject.
Below are some key differences between Service and IntentService.

When to use?

  • The Service can be used in tasks with no UI, but shouldn't be too long. If you need to perform long tasks, you must use threads within Service.

  • The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks).

How to trigger?

  • The Service is triggered by calling method startService().

  • The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.

Triggered From

  • The Service and IntentService may be triggered from any thread, activity or other application component.

Runs On

  • The Service runs in background but it runs on the Main Thread of the application.

  • The IntentService runs on a separate worker thread.

Limitations / Drawbacks

  • The Service may block the Main Thread of the application.

  • The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.

When to stop?

  • If you implement a Service, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService(). (If you only want to provide binding, you don't need to implement this method).

  • The IntentService stops the service after all start requests have been handled, so you never have to call stopSelf().

What are differences between JobIntentService and IntentService?

I would recommend to read this article explaining about the difference between intent service and job intent service. When we look for the first time at these terms Service, IntentService, JobIntentService they would look almost similar - in one way or other they would perform some operations in background (which user does not notice). But there is few difference in the way they operate,

  • Service - This runs on the same main thread which invokes this service and performs some background operation. For any long running operation happening on the main thread it is recommended to create a new thread and do the job (eg; Handler) by not impacting the main thread's performance.

    Drawback : Runs on main thread

  • IntentService - Intent service also helps in doing some long running (indefinite) background task. The only difference is that it creates a new thread to perform this task and does not run on the main thread. Does the given job on it's onHandleIntent.

    Drawback: The job given to the IntentService would get lost when the application is killed

  • JobIntentService - Job intent service is very similar to IntentService but with few benefits like the application can kill this job at any time and it can start the job from the beginning once the application gets recreated/up.

But from Oreo, if the app is running in background it's not allowed to start the service in background. Android asks us to start the service explicitly by context.startForegroundService instead of context.startService and when the service is started within 5 seconds it must be tied to the notification to have a UI element associated with it.

Reference : https://developer.android.com/about/versions/oreo/background.html

Differences in the use of IntentService and Service

Service

This is the base class for all services. When you extend this class, it’s important that you create a new thread in which to do all the service’s work, because the service uses your application’s main thread, by default, which could slow the performance of any activity your application is running.

IntentService

This is a subclass of Service that uses a worker thread to handle all start requests, one at a time. This is the best option if you don’t require that your service handle multiple requests simultaneously. All you need to do is implement onHandleIntent(), which receives the intent for each start request so you can do the background work.

Differences

Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service. IntentService creates a queue that passes one intent at a time to onHandleIntent(). Thus, implementing a multi-thread should be made by extending Service class directly. Service class needs a manual stop using stopSelf(). Meanwhile, IntentService automatically stops itself when there is no intent in queue. IntentService implements onBind() that returns null. This means that the IntentService can not be bound by default. IntentService implements onStartCommand() that sends Intent to queue and to onHandleIntent().

Multiple IntentService or one Service

IntentService is just a convenient class to write services that are workers in the producer-consumer pattern. They are services designed to execute various tasks in a row and then stop. Services are not necessarily IntentServices such as services that must stay alive such as daemons.

So you should wonder if you service is close to a worker thread, if so, use IntentServices else just derive from Service.

Your second questions was whether to group all 3 services in a 3 in 1 service. The answer is that it depends how you use your datasources : if you use them altogether, then group them in a single service. If they are used separately, you could build a service for each with the hope to provide a lighter service if only one datasource is used and not the other. But if you use all 3 datasources, each in a service, then it will be heavier than using a single service.

Service vs IntentService in a case of location tracking service

An IntentService is meant to be a fire and forget service, for relatively short tasks that may be repeated. Another important distinction is that an IntentService stops itself when onHandleIntent() returns. A regular Service doesn't stop unless you (or the android os) explicitly stop it.It sounds like you are planning on a long-running task that also runs when your app is not in the foreground.

In this scenario you definitely want to use a regular Service. You can still choose to perform the work in a separate thread by creating one inside the service and doing the work in there, but you don't necessarily have to. Remember that by default, a Service runs on the main thead.



Related Topics



Leave a reply



Submit