Service vs IntentService

Can someone please show me an example of something that can be done with an IntentService that cannot be done with a Service (and vice-versa)?

I also believe that an IntentService runs in a different thread and a Service does not. So, as far as I can see, starting a service within its own thread is like starting an IntentService . Is it not?

I would appreciate if someone can help me with both of my questions.


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


  • If someone can show me an example of something that you can be done with an intentService and can not be done with a Service and the other way around.

    By definition, that is impossible. IntentService is a subclass of Service , written in Java code. Hence, anything an IntentService does, a Service could do, by including the relevant bits of code that IntentService uses.

    Starting a service with its own thread is like starting an IntentService. Is it not?

    The three primary features of an IntentService are:

  • the background thread

  • the automatic queuing of Intent s delivered to onStartCommand() , so if one Intent is being processed by onHandleIntent() on the background thread, other commands queue up waiting their turn

  • the automatic shutdown of the IntentService , via a call to stopSelf() , once the queue is empty

  • Any and all of that could be implemented by a Service without extending IntentService .


    Service

  • Invoke by startService()
  • Triggered from any Thread
  • Runs on Main Thread
  • May block main (UI) thread. Always use thread within service for long task
  • Once task has done, it is our responsibility to stop service by calling stopSelf() or stopService()
  • IntentService

  • It performs long task usually no communication with main thread if communication is needed then it is done by Handler or BroadcastReceiver
  • Invoke via Intent
  • Triggered from Main Thread
  • Runs on the separate thread
  • Can't run the task in parallel and multiple intents are Queued on the same worker thread.
  • 链接地址: http://www.djcxy.com/p/62624.html

    上一篇: Activity仍然可以调用unbind服务方法。 这是正常的吗?

    下一篇: 服务vs IntentService