rayman
rayman

Reputation: 21596

Further explanation about IntentService

I was trying to get the purpose of the IntentService, and when should I use it?

I tried to understand from the API, but not enough details on that one.

Is there any analogy between this and running long-task on a working thread?

Any further explanation and samples on why I would use IntentService would be very welcome.

Thanks,

ray.

Upvotes: 11

Views: 6610

Answers (3)

Edmund Chang
Edmund Chang

Reputation: 257

I would use it whenever the use case is a specific task that will take a long time but has a regular ending. For example, the use cases spelled about above and also such regular tasks such as: -syncing -downloading resources -pulling data from various sources and storing them

I would not recommend using it when the service is long running in nature and doesn't have a definite end. I've seen this done and it is a mistake because the way people end up getting around the fact that IntentService stops itself is to either create locks to keep handleIntent from finishing or if they don't want to block the next requests in line, they'll restart themselves by sending startService again to themselves. This is horrible and for these type of scenarios, you should just use a long running regular Android Service and make sure it's bound to (probably AUTO_CREATE) and handle the work off the main thread.

Upvotes: 0

sravan
sravan

Reputation: 5333

Android services are the workhorses of the platform. They allow applications to run long running tasks and give functionality to outside applications. Unfortunately, since they run in the background and are not visually obvious to developers, they can be a source of major headaches. If you have seen an ANR (activity not responding) or have wondered why a service was running when it shouldn't be, a poorly implemented service could be the issue.

The dreaded ANR Let's take a look at two major errors services cause: ANRs. These disrupt the user's experience and gives the user an option to force close. This can lead to a user uninstalling your app or the background process being in a disrupted state. The reason for this is that services are launched from your calling thread-- usually this is the UI thread. Always running services. Although Android provides mechanisms for scheduling and stopping services, not all developers follow the guidelines. In fact, a lot of the examples I've seen don't even mention stopping your service. This can lead to slower overall performance, user confusion, and battery drain. I imagine the Android developers at Google saw that this was becoming an issue and introduced the IntentService class in Android 1.5. The IntentService class addresses the above issues and abstracts all of the management of threads and service stopping from the developer. It's very easy and powerful way of offloading tasks from the application's main thread.

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006614

Is there any analogy between this and running long-task on a working thread?

IntentService handles the thread for you.

when should I use it?

Some use cases:

  • For handling alarms triggered by AlarmManager
  • For handling other events caught by a manifest-registered BroadcastReceiver
  • For handling events triggered by an app widget (time-based updates, button clicks, etc.)

Upvotes: 13

Related Questions