I am working on a project that. It will connect to a remote host using tcp sockets from a service. And interact with it for getting and sending data.
The flow, I have planned is;
a singleton socket class. Which returns a connected socket refference;
an activity to ask user for remote server’s ip and port.
a service triggered(intent) by the activity with the parameters of ip:port.
service will get the socket using singleton socket class.
and then service will read a data; parse it and show user for user actions in a new action window ( intended).
after user completed the action; result will be sent to the server. and new request will be read.
problem/query is here;
-
when you come back to the service for second data read operation; will the socket be there ? (or garbage collected )
because after data read operation started no new connection accepted. -
will the first service die after calling the user actions activity ?
-
what happens if I call the service using startService from an activity, a new service created or the existing before started service called back again ?
-
how can I let the service live for ever unless I said it to die.
Sorry, If I am asking silly questions.
Here’s how I see it:
If the object is static and has the socket as a member variable it will most likely be there, you should have safeguards in your single pattern to shield from this problem
(if single==null){...}, you could also try managing it by overriding theApplicationclass and adding a factory method in there. You need to make sure the socket is open and closed correctly and not just left hanging as this could create problemsEDIT:
Whether the actual socket is still open will depend on the timeout of the socket
Services need to be told to stop and so it will not die
If the service is running, the it will not create another, it will call the
onStartCommand()of the running serviceThis is default behavior