Ahmed Ali Ahmed Ali - 24 days ago 22
Android Question

Thread vs Handler vs Async task for sockets in android?

I'm going to write an

server socket to connect with a

Which one is the best between
Async task

Answer Source

You can use both a Thread or an AsyncTask, just chose one of the two based on your needs (if you provide more details about the task you need to perform I might be able to help you more specifically).

The Handler class is not meant to be used to run asynchronous tasks (like Thread and AsyncTask), an Handler instance is used to allow communication between two threads, for instance, if you decide to use the Thread over the AsyncTask, an Handler class will help you with data exchange between your UIThread and the Thread that handles the socket.

Now, AsyncTasks are perfect for working as a Client. If what you need is opening a Server socket you should forget about those and try to have a look at the Service and choose between Service and Thread.


Because AsyncTask are meant to perform a specific job in a relatively little time. To open a server, which will need to stay opened for (possibly) a long time that's not the class you need.

I'd take a look at the Service because it provides some useful methods to interact with the Android application lifecycle.