Service
is a base class of service implementation. Service
runs on the application’s main thread which may reduce the application performance. Thus, IntentService
, which is a direct subclass of Service is available to make things easier.
The IntentService
is used to perform a certain task in the background. Once done, the instance of IntentService
terminates itself automatically. Examples for its usage would be to download a certain resource from the Internet.
Differences
Service
class uses the application’s main thread, whileIntentService
creates a worker thread and uses that thread to run the service.IntentService
creates a queue that passes one intent at a time toonHandleIntent()
. Thus, implementing a multi-thread should be made by extendingService
class directly.
Service
class needs a manual stop usingstopSelf()
. Meanwhile,IntentService
automatically stops itself when it finishes execution.IntentService
implementsonBind()
that returnsnull
. This means that theIntentService
can not be bound by default.IntentService
implementsonStartCommand()
that sends Intent to queue and toonHandleIntent()
.
In brief, there are only two things to do to use IntentService
. Firstly, to implement the constructor. And secondly, to implement onHandleIntent()
. For other callback methods, the super is needed to be called so that it can be tracked properly.