11

I am working on an Android application that requires a TCP connection to a TCP server(Written in Node.js)

My Android TCP client is working an can send messages back and forth.

My spesific questions is:

  1. What is the best way to handle a TCP connection to a server in Android ?
  2. How to maintain the connection(close the connection properly on onDestroy() etc) ?
  3. Is there any bether way then using an AsyncTask(Except a normal Thread class, which is not allowed in Android 4.0)

I have my socket connection in an AsyncTask like this:

@Override
protected Void doInBackground(Void... params) {
        try {
            Log.d("TCP_MESSAGE", "Connecting...");

                socket = new Socket(MY_LOCAL_IP, 8080);
                dataOutput = new DataOutputStream(socket.getOutputStream());

                inputstream = new InputStreamReader(socket.getInputStream());
                input = new BufferedReader(inputstream);


            while (!canceled) {
                String message = input.readLine();
                handleMessage(message);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

The reason i have the connection in an AsyncTask is because i am using android 4.0 and are not allowed to have network code in a regular Thread.

Mike Pennington
  • 38,579
  • 16
  • 126
  • 167
Emil
  • 146
  • 1
  • 1
  • 5
  • 2
    Are you saying that using Thread is not allowed in 4.0? That is not true. But anyway see my answer below. – Tobias Ritzau Oct 19 '12 at 13:20
  • You're wrong about having threads in 4.0. What you can't do is have the network code in the **MAIN** thread. – m0skit0 Oct 19 '12 at 13:26
  • Okey. I got an exception when i used a normal Thread(And found other threads that spesified the same problem). But i did thought it was a bit strange, så i guess you have right. – Emil Oct 19 '12 at 19:23

2 Answers2

6

A Service should own the connection. If you need to keep the connection while your app is not running, then you should make it a foreground service. If you don't make it a foreground service, make sure to bind to the service from your activities to keep it alive.

Don't forget that the service also runs on the main (UI) thread so you still need a separate thread to handle the communication.

If you only have one activity and just want to handle restarts due to configuration changes, then you can handle the configuration changes yourself, keep the connection owned by a non-ui fragment, or pass the connection to yourself using onRetainNonConfigurationInstance()/getLastNonConfigurationInstance() (however that is deprecated in favor of using fragments).

Tobias Ritzau
  • 3,259
  • 2
  • 14
  • 28
  • I don't know why you're talking about Service. He didn't mention Service anywhere, either he talked about maintaining the connection when app is dead. In fact he states that he wants the connection destroyed in the `onDestroy()` callback... – m0skit0 Oct 19 '12 at 13:27
  • 4
    No, but he asked about the best way to keep a connection, and that is a service if you want to keep the connection between activities and configuration changes. – Tobias Ritzau Oct 19 '12 at 13:29
  • 1
    The best way to keep a connection **in that situation** – m0skit0 Oct 19 '12 at 13:31
  • 1
    You definitely shouldt not close the connection in `onDestroy()` Since you never know if it is called. – Tobias Ritzau Oct 19 '12 at 13:31
  • 2
    Tobias is right, you don't want the connection to close when the screen orientation is changed, do you? –  Feb 12 '13 at 09:07
  • a Service seems a rather heavy-handed method for simply maintaining a TCP connection. – Michael Sep 25 '14 at 16:21
  • I have used Application class to maintain the connection through Activites... and its working fine. – Pramod Dec 01 '14 at 12:41
2

What is the best way to handle a TCP connection to a server in Android?

This heavily depends on the complexity of your app, and what you want to do with it. There are no specifics for Android networking.

How to maintain the connection(close the connection properly on onDestroy() etc) ?

Open the socket whenever you fell to and keep it as a class field. Close it when onDestroy() is called. Make sure you check if it's not null first.

Is there any bether way then using an AsyncTask(Except a normal Thread class, which is not allowed in Android 4.0)

You can use normal Java threads in Android 4.0 (API14). You might have a confusion: what is not allowed in Android is having network code in the main thread (UIThread).

m0skit0
  • 23,052
  • 11
  • 71
  • 113
  • Yes, i was wrong about the threads in Android 4.0. I close my connection on onDestroy(), but closing threads i not allways that easy. But thanks for your reply. – Emil Oct 19 '12 at 19:31