Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 31 March 2016

Deference between service and intentService


In my words
1-->

Service

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.

IntentService
Service is a perform long-time background operation and stop manually but IntentService is a perform short-time operation this is depend on user forwarding intent data if intent queue is null this is automatically finish.

2-->
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.  

3-->
  1. Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service.                                                                                            
  2. 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, IntentServiceautomatically stops itself when it finishes execution.                                                                                                                         
  3. IntentService implements onBind() that returns null. This means that the IntentService can not be bound by default.                                                                                                                             
  4. IntentService implements onStartCommand() that sends Intent to queue and to onHandleIntent().

Other Way


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 calling to method onStartService().
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 may be triggered from any thread.
The IntentService must be triggered from Main Thread.

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.




Thank you