val startTime = SystemClock.elapsedRealtime()// do somethingval elapsedInMilliSeconds = SystemClock.elapsedRealtime() - startTime
TLDR: SystemClock.elapsedRealtime
(times since boot) is more reliable as SystemClock.currentTimeMillis
can be changed (by user or network).
SystemClock.elapsedRealtime return the time since the system was booted, and include deep sleep. This clock is guaranteed to be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend basis for general purpose interval timing.
System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock.