Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 765
rayman Avatar asked Jun 15 '10 13:06

rayman


2 Answers

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.)
like image 140
CommonsWare Avatar answered Sep 28 '22 02:09

CommonsWare


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.

like image 23
sravan Avatar answered Sep 28 '22 02:09

sravan