Reputation: 129
I have two AsyncTask in my app, MyClientTask and CloseSocket. MyClientTask is to connect to a server and output whatever the server sends in a textview and with a notification, which I press a button that connects and sends a message to the server. It works fine. Now, for CloseSocket, I wanted to have a disconnect button that stops the connection. It is set up the exact same way as the connect button. It is a different AsyncTask of course but it is never called. There are no errors. I put a simple System.out when I declare the button to see if its set up correct, which works. I also have a System.out in the "doInBackground" that will let me know if it ever gets called, which it does not. I have
socket.close(), socket.getInputStream().close(), socket.getOutputStream().close()
to close the socket, but it doesn't matter because the CloseSocket AsyncTask never gets called.
Also, The server is pinging the app every 10 seconds for testing so I don't know if that has something to do with it.
Upvotes: 0
Views: 116
Reputation: 8695
As of Honeycomb, AsyncTasks run serially by default, not in parallel. You need to tell it to run on a ThreadPoolExecutor.
if (Build.VERSION.SDK_INT >= 11) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
task.execute();
}
Upvotes: 3