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, AsyncTask
s 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
.
Why?
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.
3
solved Thread vs Handler vs Async task for sockets in android? [duplicate]