TCP Socket Connection as a Service

I searched a lot and gave up. I have a TCP client class that basically connects to the server. I call this tcp client class from my main activity and use the asynchronous thread from the main activity. I run the method in the tcp client class and everything is going well. I have another action that needs to be sent by calling a method in this tcp client class, but I cannot do this. please, help.

I was thinking of converting my tcp client class as a service, but in this case I'm not sure how to maintain a running method that listens for incoming messages. please, help.

I used this client class TCP and the same main activity http://myandroidsolutions.blogspot.pt/2012/07/android-tcp-connection-tutorial.html

I also need to enable the function of sending images through this TCP.

+4
source share
1 answer

Put your TCP connection code in the service, then connect to the Activity, if necessary. For example, see this

So, what happens when you need your main activity, it will connect to this service and disconnect when it goes into onPause state, and another action, which is in the foreground, will take over the management of the service and do what you need to do,

+2
source

All Articles