轉自:http://blog.csdn.net/fangyoayu2013/article/details/50786611
Android中計算時間間隔的方法:
記錄開始時間 startTime,然後每次回調時,獲取當前時間 currentTime,計算差值 = currentTime - startTime,而獲取當前時間
Android系統提供的兩個方法:
SystemClock.uptimeMillis 和 System.currentTimeMillis
兩個方法之間的區別:
SystemClock.uptimeMillis() // 從開機到現在的毫秒數(手機睡眠的時間不包括在內);
System.currentTimeMillis() // 從1970年1月1日 UTC到現在的毫秒數;
存在的問題:
System.currentTimeMillis() 獲取的時間,是可以通過System.setCurrentTimeMillis修改的,那麼,在某些情況下,一但被修改,時間間隔就不準了。
特別說明
AnimationUtils的解釋中對這個問題進行了闡述:
/**
* Returns the current animation time in milliseconds. This time should be used when invoking
* {@link Animation#setStartTime(long)}. Refer to {@link android.os.SystemClock} for more
* information about the different available clocks. The clock used by this method is
* <em>not</em> the "wall" clock (it is not {@link System#currentTimeMillis}).
*
* @return the current animation time in milliseconds
*
* @see android.os.SystemClock
*/
public static long currentAnimationTimeMillis() {
return SystemClock.uptimeMillis();
}