0

I am developing chat app and saving currentTimeMilis . I am getting new entrys sort by currenttimemilis . when new entry come listview updates . but some peoples currenttimemilis value is grater than other .. So it is problem for show new entrys

So i cant get new entrys ... What i should to do ?

Renato Lochetti
  • 4,558
  • 3
  • 32
  • 49
metin
  • 33
  • 1
  • 6
  • Are the currentTimeMilis generated in each decentralized client? – Renato Lochetti Sep 30 '12 at 14:38
  • 1
    currentTimeMillis will depend on the users system clock. Not everyone keeps theirs accurate. You cannot depend on it to compare times between devices. – Simon Sep 30 '12 at 14:45
  • @RenatoLochetti Yes .. currentTimeMilis generated in each decentralized client .. they could be different country or city – metin Sep 30 '12 at 23:04
  • @Simon can i set clock to GMT for only my app is running ? – metin Sep 30 '12 at 23:05
  • @metin As everyone told, every user need to send a time that is based in one server, for example. This way you'll be able to sort the entrys in a proper way. – Renato Lochetti Sep 30 '12 at 23:26
  • @RenatoLochetti it is a good way but I am using Cloud API and API is not giving any server time or like that value . so i am responsible for sync . – metin Sep 30 '12 at 23:52

1 Answers1

0

If timming is being collected at different devices they will always be different from device to device as they are not synchronized with each other. I see only to options to solve it:

-If messages are being sent immediatly, you can set the message time as the time of message arriving, as they will "probably" arrive by the order they have been sent. The "probably" is because some user may have a slower internet connection which may delay the message, but it shouldn't be significative for most uses.

-If messages are being sent later in time, you would have to connect to a time server, get the time from there to set in the message. Of corse, you wouldn't need to connect for every message, just on start of the application and get the time difference between the device and the server, and then apply that difference for every message.

-If your application requires GPS to be enabled, you can also get the exact time from it. As GPS uses UTC, you would need to apply the correction for your timezone.

--EDITED--

Some more detail on how to get the time difference from a time server:

1-On application start you connect to a time server to get the exact time and compare that time with the device time. You store the time difference in a variable to use later.

2-Every time you want to send a message using Cloud API, you get the device time, add the time difference obtained in step 1 above and set the message time to this value. You may also need to apply corrections related to different time zones, if you have users in different contries.

good luck.

Luis
  • 11,978
  • 3
  • 27
  • 35
  • thanks to answer but i am logging only currentTimeMilis by giving each client and i am not directly connect server .. I am using Cloud API and API is not giving any server time or like that value. – metin Sep 30 '12 at 23:08
  • You may get the time from a different server. See the question on that subject: http://stackoverflow.com/questions/11737704/java-android-get-atomic-time-from-itnernet-server. I'm editing my answer to add GPS as a solution for getting the time. – Luis Sep 30 '12 at 23:16
  • my app isnt use GPS . and i looked your link , it is good way for server but i am using API , not directly connecting server – metin Sep 30 '12 at 23:55
  • You should keep using the API for the Cloud and add the code to get the exact time difference in the application initialization. With that you can set the correct time for the messages when you use the Cloud API. – Luis Oct 01 '12 at 00:27
  • i am not sure i am understand you fully but get something.. i will try and if its work i will as an answer your reply. thansk – metin Oct 01 '12 at 00:48