So, I end up with this script. In this scenario, the user most often connects to the Internet only once for each reboot of the device.
I try to connect to the NTP server when the user boots up ( BOOT_COMPLETED BroadcastReceiver
) or the network status changes ( CONNECTIVITY_CHANGE and WIFI_STATE_CHANGED BroadcastReceiver
), and then I save this date SystemClock.elapsedRealtime()
in the database as well. After that, when I want to have the correct date, I just get a new one SystemClock.elapsedRealtime()
and calculate the difference of this value and the elapsedRealtime value from the database and add this difference to the date that I store in the database.
source
share