时间间隔: SystemClock.uptimeMillis与System.currentTimeMillis

来源:互联网 发布:mars建筑软件 编辑:程序博客网 时间:2024/04/20 06:36

很多情况下,不管是我们自己使用时间间隔来做一些算法,或是调用系统的API,比如动画效果,都会需要基于时间间隔来做,通常做法是:记录开始时间 startTime,然后每次回调时,获取当前时间  currentTime,计算差值 = currentTime - startTime,而获取当前时间,系统提供了两种方法:

SystemClock.uptimeMillis 和 System.currentTimeMillis

这两种方法有何区别呢?

1. SystemClock.uptimeMillis()  // 从开机到现在的毫秒数(手机睡眠的时间不包括在内);

2. System.currentTimeMillis() // 从1970年1月1日 UTC到现在的毫秒数;

但是,第2个时间,是可以通过System.setCurrentTimeMillis修改的,那么,在某些情况下,一但被修改,时间间隔就不准了。

特别说明点:AnimationUtils 中明确说了:

[java]
/**
 * 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
 */ 

0 0