I got slightly confused about the differences between Handlers
, AsyncTask
and Threads
in Android. I’ve read quite a few blogs and questions here in StackOverflow.
Handler
are background threads that provide you to communicate with the UI. Updating a progress bar, for instance, should be done via Handler
. Using Handlers you have the advantage of MessagingQueues
, so if you want to schedule messages or update multiple UI elements or have repeating tasks.
AsyncTask
are similar, in fact, they make use of Handler
, but doesn’t run in the UI thread, so it’s good for fetching data, for instance fetching web services. Later you can interact with the UI.
Thread
however can’t interact with the UI, provide more “basic” threading and you miss all the abstractions of AsyncTask
.
However, I would like to have a socket connection run in service. Should this be run in a handler or a thread, or even an AsyncTask
? UI interaction is not necessary at all. Does it make a difference in terms of performance which I use?
Meanwhile, the documentation has been majorly improved.