-2

When I have to use IntentService?

And when I have to use Service?

I don't know what are they used for.

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87

1 Answers1

1

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().

Kiloreux
  • 2,220
  • 1
  • 17
  • 24