Sync Android x Web

Asked

Viewed 301 times

5

I am developing an application that will work offline on android and sync with a version web. I have a API that will mediate this synchronization.

Currently the version android has a timestamp to control the changes in the record, and the idea was to synchronize with the API check records with an update date longer than the last sync date.

But doubt is like controlling changes in the clock android because the user may have a different time or date than the correct one. So it could occur that the time of the device is shorter than the last synchronization causing it not to send the changed data.

  • 1

    When starting the Android app ask the web service your UNIX Datetime, do the same with the android device calculate the difference and apply it to calculate the timestamp.

  • @ramaral your answer seems correct, why not put as an answer, so Alisson can accept as correct.

  • @Túliof. Maybe it’s not completely correct because it only works if the app has internet access.

1 answer

4


The method System.currentTimeMillis() returns how many milliseconds have passed since midnight on 01/01/1970. The problem is that it can be changed by the user or the cellular network itself.

Your application can detect when that clock is changed by listening to the following Broadcasts:

The biggest problem is that you have no control over the device clock, knowing this you can think of a logic that meets your need, can be simple or complex.

An alternative

In case time is not important, instead of using timestamp, why not use a version number? This eliminates all the problems a timestamp has.

Incremental

May incremental, your server sends a version number, e.g..: 1, in the next update 2 and so on. The App compares the version when synchronizing, updates if you have a smaller number than the server.

Unique identifier

Or it could be a unique identifier, as a GUID java.util.UUID.randomUUID();. Your server sends this guid. The App compares if the guid it has is different from the server, updates if it is different.

  • Tulio, I had already solved this case. I used the option of incremental. I created a field that every change you have is incremented, so I can know if there has been a change since the last sync.

  • @Alissonmarqui in this case the OS encourages you to post an answer to your own question and accept it as correct. So that other people who have the same doubt can benefit from the solution you found.

  • I will post the reply here. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.