Reputation: 11975
I want to create a little app that is supposed to send some JSON data to a remote server.
I need take care of situations like when the user has no internet or just the data can't be send, so the phone retries until that data has been sent from the phone and received by the server.
Is there a pattern like this? General pattern or Android specific?
Thanks a lot in advance!
Upvotes: 2
Views: 318
Reputation: 21
I'm using a timeout timestamp to know if a Message (in this case it's for a serial communication) was not sent:
private TimerTask WriterTask = new TimerTask() {
@Override
public void run() {
wasStarted = true;
synchronized (MessageQueue) {
if (mSIOManager != null && mSIOManager.getmWriteBufferSize() == 0
&& MessageQueue.size() > 0) {
QueueEntry item = MessageQueue.peek();
if (item != null && !item.sent) {
timeoutTimer = System.currentTimeMillis();
mSIOManager.writeAsync(item.Msg.getBytes());
Log.v(TAG, HexDump.dumpHexString(item.Msg.getBytes())
+ " - Written to Buffer");
item.sent = true;
}
}
try {
if (System.currentTimeMillis() - timeoutTimer > TIMEOUT && MessageQueue.peek().sent) {
Log.i(TAG, "Message timed out: " + HexDump.dumpHexString(MessageQueue.poll().Msg.getBytes()));
}
} catch (NullPointerException e) {
// Queue empty
}
}
}
};
So instead of Logging the Event you could increase the scedule rate for the Task from ie. 100 to 10000ms
Timer mTimer = new Timer();
mTimer.scheduleAtFixedRate(WriterTask,100L,10000L);
If your data was finally sent you just cancel the schedule
mTimer.cancel();
Upvotes: 1