We are working on a distributed system database syncing, which relies on the real time.

As the device's time can be changed by the user or a faulty network, it can corrupt the whole database very easily. So we decided to get the time from the Master server, store it, and calculate the real time by calculating the time interval passed by counting the number of clock ticks. How can we get access to the hardware's clock ticks. And will we have to run some service for this? We want the ticks to be counted even while the device is on stand by/sleep/switched off. Like the actual clock displayed. We want to it to be a sort of a parallel clock, just that only the Master server will be able to set time on it.

有帮助吗?

解决方案

How can we get access to the hardware's clock ticks.

You don't.

We want the ticks to be counted even while the device is on stand by/sleep/switched off.

Your users do not. Your users want their devices to behave normally, rather than having some software keeping the CPU powered on and tying up RAM, just watching the clock tick, because that will drain the battery fairly quickly.

Perhaps things are different in "remote areas and villages" near you. In most of the known portions of the universe, time elapses even if your app is not running.

Hence, you do not need to "count ticks" to determine if time elapses. Instead, when you "get the time from the Master server" and "store it", you also store the current value of SystemClock.elapsedRealtime(), which is the number of milliseconds since the phone was last rebooted. Then, when you need to determine the time relative to "the Master server", you compute the difference between the stored elapsedRealtime() value and the current elapsedRealtime() value, and add that number of milliseconds to the "time from the Master server" to get the current time as seen by "the master Server".

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top