Asaf Nevo
Asaf Nevo

Reputation: 11688

Background server approach

I have built an Android chat application. It has a TCP server which is connected to via using a Socket.

I want the server connection to work in the background always - even if the app is not in foreground - and listen to data sent from the server.

For now I am using a while loop to checking if there is something new, but the OS close my thread when Android needs resources.

I have thought about using the readline() method in my thread instead of using the while loop because readline() is a blocking method.

Is this the right approach to take, which will lead to, prevention of the OS from killing my application?

Is there any way, like a custom broadcast receiver that will launch only when the incoming socket is available?

Thanks!

Upvotes: 2

Views: 1051

Answers (1)

t0mm13b
t0mm13b

Reputation: 34592

In short, Android can and will kill any applications that hog up resources in order to keep running. The onus is on you on how to handle the scenarios which can threaten your app to be killed.

I suggest looking at the service's life-cycle as found on the developer's site.

For a start, any application, be it service/activity, that hogs up too much, in this manner is considered... "rude" in the eyes of Android, and therefore, is prepared to be killed in this manner regardless!

For example, listen in the onLowMemory override method and act accordingly, such as saving data and what-nots.

What really, should be happening, is this, the service, spawns a thread to periodically listen for incoming connections in this manner,

while (!terminating){
     Socket incomingClientSocket = ourServerSocket.accept();
     // How you handle this part is up to you?
     Thread.sleep(500); // Sleep for 500 ms
}

The terminating variable is a boolean and is the deciding variable that controls when to terminate the loop. Notice how the Thread.sleep method is used, to "calm down" the execution, if omitted, Android will put a cross-hair on the app and terminate it, in other words, be polite to system resources.

Hint: Extend the Thread class, to hold the functionality of handling incoming connections via the accept() method.

When the incomingClientSocket becomes non-null, then another thread is created in which it opens the input/output stream of the incomingClientSocket and read/write to it using binary fashion.

Do not use the readline method as that is an indication of poor design and assuming the data is text-ual, because one incoming packet to the client could be for example, 720bytes, the next packet coming in after that, could well be 564 bytes, that is the nature of TCP/IP.

You need to come up with a more reliable way of establishing boundaries for the data transmission, for example, a begin and end marker, and let the server read the incoming data and distinguish the byte stream that composes of a begin and end markers, once both are present, then extract the real data in-between the markers and act on it accordingly.

You could for instance, say, upon incomingClientSocket that actually becomes non-null, send a broadcast to your activity to act on it, and let the activity, take that socket, and open the connection and read/write to/from the input/output streams associated with the socket.

Upvotes: 1

Related Questions