Can someone tell me the TRUE difference?
That's like asking the difference between a shovel, a hammer, and a nailgun. They're all tools, but they're only loosely related, some looser than others.
At the risk of sounding self-serving, you really need to pick up a book.
In a nutshell:
- A service is an Android component that lives independently from any other components. Activities may come and go, but services can stick around, if you wish. They serve a number of roles, from managing state that multiple activities rely upon to serving as cron jobs to handling longer app widget updates.
AsyncTask
is a class that arranges to do some work off the main application thread. From the implementer and user of theAsyncTask
, how it performs that bit of magic is not generally important. In reality, it uses a thread pool and work queue.AsyncTask
uses aHandler
to help arrange for select bits of work to be done on the main application thread (e.g., notifying the user of progress updates).Handler
is a class that is tied tightly into the message queue and loop that forms the backbone of the main application thread. The primary purpose of aHandler
in application code is to arrange for messages to be sent from background threads to the main application thread, mostly to arrange for updates to the UI, which cannot be done from background threads.
Use Service when you've got something that has to be running in the background for extended periods of time. It's not bound to any activity. The canonical example is a music player.
AsyncTask is great when some stuff has to be done in background while in the current activity. E.g. downloading, searching for text inside a file, etc.
Personally I use Handlers only to post changes to the UI thread. E.g. you do some computations in a background thread and post the result via handler.
The bottom line: in most cases, AsyncTask is what you need.
My rule of thumb is that an AsyncTask
is for when I want to do something tied to single Activity
and a Service
is for when I want to do something that will carry on after the Activity which started it is in the background.
So if I want to do a small bit of background processing in the Activity
without tying up the UI I'll use an AsyncTask
. I'll then use the default Handler
from that Activity
to pass messages back to ensure updates happen on the main thread. Processing the updates on the main thread has two benefits: UI updates happen correctly and you don't have to worry so much about synchronisation problems.
If for example, I wanted to do a download which might take a while I'd use a Service
. So if I went to another Activity
in my application or another application entirely my Service
could keep running and keep downloading the file so it would be ready when I returned to my application. In this case I'd probably use a Status Bar Notification once the download was complete, so the user could choose to return to my application whenever was convenient for them.
What you'll find if you use an AsyncTask
for a long-running process it may continue after you've navigated away from the Activity
but:
- If the
Activity
is in the background when your processing is complete you may have problems when you try to update the UI with the results etc. - A background
Activity
is far more likely to be killed by Android when it needs memory than aService
.