I am designing an application that needs to update a server at regular intervals, but I am unsure of how to go about sending these updates.
This is what I am trying to do:
When my Activity starts, I must send an initial connection message to the server. After the connection has been established, I will send one new message to the server every second. When the user performs some action that the server needs to know about, that message will be added to a queue. Every second or so, my app would then check the queue for messages to send. If one is available, it is sent. If the queue is empty, a simple Keep Alive message should be sent to maintain the connection.
My question is, what would be the best strategy for me to implement this? Should I be using a separate Thread? Or a Service? Or AsyncTasks?
My thoughts so far: The connection does not need to be maintained past
the lifecycle of the activity, so it seems to me that a Service would
be overkill.
And how should I handle scheduling the updates? Should I be using a MessageQueue with a Looper? Or a ScheduledExecutorService? Or a Handler?
My thoughts so far:
My understanding is that MessageQueue blocks until it receives a
message to send.
I’m not sure this is what I want. My plan was just to send Keep
Alive messages
when the queue is empty, not block until a new message arrives.
I’m totally new to this sort of stuff so I might be going about this entirely the wrong way. I’d be happy to hear any advice you can offer.
I would use a separate thread with a sleep statement (or android equivalent) … keeping it as simple as possible is necessary with your intended timings and the fact that the app is not intended for unattended execution