0

I recently did a project on Android 3.2 to send strings to a server in my PC with a TCP connection. I had to do a thread to achieve this, something that I didn’t have to do in a Java program. I also used the AsyncTask method, but I couldn’t send information with other functions besides the doInBackground function, in which I created the socket connection.

To send the strings, I created a loop in the doInBackground function, so it was waiting to receive a new message to send to the computer all the time. The problem with this is that I can’t do bidirectional communication. After this project, I continued to try to send the string with other function besides the doInBackground function in which. Finally, I was able to succeed with Android 1.6.

It’s a shame that this method can’t be used in superior versions of Android, because of the need of threads and also that you need to send the message in the same function you create the socket connection.

I was wondering if there is a better way to perform duplex operations in newer versions of Android.

CJBS
  • 15,147
  • 6
  • 86
  • 135
Barscunes
  • 3
  • 2

1 Answers1

0

You can use Threads in any android version.

The right way to do what you want, would be to create a Service ( http://developer.android.com/reference/android/app/Service.html ) and use separate Threads in the Service to send and to recieve data.

Adam Monos
  • 4,287
  • 1
  • 23
  • 25
  • Thanks for the answer. Today i was studding about service and handler, I have to study more and try to do an application This [link](http://stackoverflow.com/questions/5135438/example-android-bi-directional-network-socket-using-asynctask) shows the same method I used for my application. For me it only worked as bidirectional in android 1.6, and as only client with the loop in do it background function in posterior os. I’m just curios about why didn’t work in posterior os, because I have seen this example in many places. But I will study what you told me, and see the differences. – Barscunes Aug 02 '12 at 06:17
  • Well, the example what you just posted doesn't leave the connection open. It will close after it read the data sent by the server. If the server doesn't send anything, it will close almost immediately and you won't be able to send anything either. You have to keep the connection open, and listen to incoming data, but `AsyncTask` is not the way to achieve this, but a `Service` with background `Thread`s is. – Adam Monos Aug 02 '12 at 07:10
  • Mm basically what I understood, it’s that the service it’s used when the background process you have to do it's long (because in my case I don’t need to use the service from other application), it’s the opposite of the AsyncTask that it’s used in small background process. The service doesn’t have a thread but you can create one in there to create the tcp/ip conection. I was playing with the example of AsyncTask and in the manifest I change the minSdkVersion from 10 to 8 and it works. – Barscunes Aug 06 '12 at 02:18
  • I have to work on an algorithm to create a bidirectional communication without using different functions because in sdk higher versions than 8 you can only establish the communication in the same place you create the connection. Or like you sayed, to create two threads one to recive and other to send, but I have to try it. Well basically I believe that, I don’t know if I didn’t get your point, and again thank you for all your help. – Barscunes Aug 06 '12 at 02:19