Why use a Service over an IntentService?

674 views Asked by At

Why does the SyncService example provided by Google below use a Service instead of an IntentService? It is my understanding that IntentServices run in the background, while a regular Service will run in the main thread. For something with no UI that just updates data, why would you want it run in the main thread? Doesn't that risk frame-drops?

http://developer.android.com/training/sync-adapters/creating-sync-adapter.html#CreateSyncAdapterService

Is it possible to have a standalone IntentService? or does it need to be based off something running on the main thread? This is the only reason I can see why we would use a regular Service above.

1

There are 1 answers

3
CommonsWare On BEST ANSWER

It is my understanding that IntentServices run in the background, while a regular Service will run in the main thread.

Objects do not run on threads. Methods do.

IntentService inherits from Service. The main lifecycle methods on Service, notably onStartCommand() are called on the main application thread. IntentService happens to provide a background thread, which it uses to call your onHandleIntent() method, triggered by a call to onStartCommand().

For something with no UI that just updates data, why would you want it run in the main thread?

You don't.

Why does the SyncService example provided by Google below use a Service instead of an IntentService?

Because an IntentService is inappropriate here. Quoting the documentation that you linked to:

To do this, you need to create a bound Service that passes a special Android binder object from the sync adapter component to the framework. With this binder object, the framework can invoke the onPerformSync() method and pass data to it.

IntentService and the binding pattern do not work well together.

From a threading standpoint, onPerformSync() is called on a background thread, supplied by Android. Hence, even if IntentService were not ruled out based upon binding, you do not need another background thread, since onPerformSync() is already called on a background thread.