How to protect the application from spoofing local time?

The situation is the following. There is an application which provides the ability to work without Internet connection, at this time, all operations in the application are recorded at the time of the phone, and after the emergence of networks all together is sent to the server.
To attach broadcastreceiver on ACTION_TIMEZONE_CHANGED and ACTION_TIME_CHANGED I know, but it works post factum - i.e., after the user changed the time and it is not clear how to know how much time left.
Throw the idea of implementation.
June 5th 19 at 21:14
2 answers
June 5th 19 at 21:16
Why not use the time server.
For example

When establishment of the connection, sinhroniziruete time.
object ServerTime {

 private var serverOffset: Long = System.currentTimeMillis() - SystemClock.elapsedRealtime()

 fun getTime(): Long = SystemClock.elapsedRealtime() + serverOffset

 fun syncTime(serverTime: Long) {
 serverOffset = serverTime - SystemClock.elapsedRealtime()

because an employee unplugged the network, turned the clock for 1/2/5 hours ago - stumble it is necessary in the application moved forward 1/2/5 hours.. III? and everything is nice. just was not the type of network. - Mckayla.Scho commented on June 5th 19 at 21:19
It is necessary at least once to serverOffset after booting the device and save it in the settings, then change the time with network or without, or does not affect. Because elapsedRealtime returns the time since system boot and intersects with the time settings. - otto16 commented on June 5th 19 at 21:22
thanks , will think about this topic :) - Mckayla.Scho commented on June 5th 19 at 21:25
June 5th 19 at 21:18
You need to fix the time intervals(e.g. every minute) - then error will be equal to the gap.
Every time schityvaetsya from the repository if the difference is greater than 2 gap means adjustment ince update the time in the store.
Yes, there is a thought. But maybe a better idea :) - Mckayla.Scho commented on June 5th 19 at 21:21

Find more questions by tags Android