1

I know we can use System.currentTimeMillis(); to get current time in Android.

But if I use that code, when user change their time manually, the value will change accordingly.

Aside from getting current time by API from backend (considering that will be low signal in sudden), isn't there any Android API I can use to detect time changes or the real current time?

Rendy
  • 5,572
  • 15
  • 52
  • 95
  • if he is having internet . You can get the time from internet. – Zar E Ahmer Sep 04 '14 at 14:31
  • Yeah I know that option. But considering that my app is only used in one place only and I am afraid that because too many people there, the signal or internet connection will be bad in sudden. – Rendy Sep 04 '14 at 14:34
  • You can't. The user can change the time to whatever they want and you don't have any control over it. Internet time is probably the only option that you _can_ control - especially if the time is coming from your server and not from a third-party. – ChuongPham Sep 04 '14 at 14:39
  • if you have an internet connection then follow this url to get the standard time from internet http://stackoverflow.com/questions/13064750/how-to-get-current-time-from-internet-in-android – Hasnain Sep 04 '14 at 14:54

3 Answers3

1

isn't there any Android API I can use to detect time changes or the real current time?

No. From the standpoint of the OS, what the user sets the time to be is "the real current time". It is the user's device, not yours.

You are welcome to consider something else to be "the real current time", but any such time source will be outside of the device, such as GPS signals, SNTP time servers, etc.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You can try SystemClock.uptimeMillis() or SystemClock.elapsedRealtime() if you aren't interested in the actual time.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • i still can't think a solution that I can cxombine with upTimeMillis or elapsedRealTime. – Rendy Sep 04 '14 at 14:54
0

In theory you could do something of the following:

  1. Record "the real current time" at some point during your application life cycle (on startup, first connection to the internet), and compare to device time, and record your delta time (time difference)

  2. Hook into user changes of date/time..see stackoverflow post: System datetime has changed event, and update your new delta time.

Seems relatively straight forward (in theory). So at least if backend is not available you can rely somewhat on this delta time.

Community
  • 1
  • 1
Ed Manners
  • 459
  • 4
  • 8
  • Yes I thought about that solution too but ACTION_TIME_CHANGED is not really reliable as CommonsWare mentioned in other post. – Rendy Sep 04 '14 at 15:25