Two possible methods:
SystemClock.elapsedRealtime()
System.currentTimeMillis()
Details:
long startTime = SystemClock.elapsedRealtime();
long endTime = SystemClock.elapsedRealtime();
double elapsedSeconds = (endTime - startTime) / 1000.0;
long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
double durationTime = (endTime - startTime) / 1000.0;
They may look like the same, but according to this:
SystemClock Document says about elapsedRealtime()
: 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.
And 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, 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.
网友评论