1

I am developing an application in Android that connects to a server using TCP Sockets. I am using AsynkTask to connect with the application.

The Application does not disconnect from the server in his entire life cycle. Even if the application goes to sleep the AsynkTask keeps running and the socket is always connected.

The Socket is disconnected only when the server closes the socket (because an exception is throwed and the doInBackground() finish )or when the user exit the application (because the AsyncTask is cancelled)

This is my doInBackground()

    @Override
public Void doInBackground(Void ...pa){

        try {                                                    
            sock = new Socket(InetAddress.getByName(serv), port);            
        }
        catch(IOException ex) { errMessage = ex.getMessage() + "\n"; }



    try {
        if(sock != null) {                

            inStr = new DataInputStream(sock.getInputStream());
            outStr = new DataOutputStream(sock.getOutputStream());

            while(!isCancelled()) {
                if(((hbDataSize = inStr.read()) & 0xC0) == 0xC0){ 
                    lbDataSize = inStr.read(); 
                    packetSize = (hbDataSize << 8 & 0x3F00) + lbDataSize; 
                    buffer = new byte[packetSize-2];
                    inStr.read(buffer, 0, packetSize-2);
                    publishProgress(0);
                }
            }

        } else {               
            throw new IOException(errMessage);
        }
    }catch(IOException e) { // sin conexion salta a a excepcion
        errMessage = e.getMessage();
        publishProgress(2);
        isError = true;
    }
    return null;
}

Anyway i just put the code so you can see the method.

I start reading about AsyncTask and it says that i should use an AsyncTask for long periods of time.

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

My app works fine, but should i change the AsyncTask for a Executor, or a ThreadPoolExecutor or a FutureTask ? And if you think i should change it, why should i change it? I've been reading about those three clases, but i am not shure if i should change it.

mavi
  • 1,074
  • 2
  • 15
  • 29
  • From the usage, it even looks more like a bound service thing? – SergeyA Jun 16 '16 at 15:56
  • Future/Threadpool/Executor are useful if you have more/many tasks that may run longer or complex threading with limits/task dependencies and so on. The _running for long periods_ means f.e. that multiple long running tasks could be started and Executor/Threadpool/Future can be used to control the number of real concurrently operating threads, execution order, inline execution and so on. A _Task_ is more a finite operation/job like _calculate x_, _download file_ and so on. Your case looks - like SergeyA wrote - more like a (Bound) Service or simply a separate thread that keeps running. – makadev Jun 16 '16 at 16:38
  • Thank you for your answers, i will use a service for my operation – mavi Jun 16 '16 at 16:57
  • @mavi- seet this [link](http://stackoverflow.com/questions/34404728/continuously-running-socket-io-service-in-android/34408691#34408691). it will be useful for you – Mangesh Sambare Jun 18 '16 at 10:57

1 Answers1

4

If you are doing a continuous job like what you described, its best you use a service, android will manage the resources better with a service. As you said even if the application goes to sleep, (did you mean the device?). The services framework or package was designed to do such things. AsyncTask is for minor download operations, like images and other stuff. But if you depend on a continuous stream from cloud services then use a service.