Android中计算时间间隔的方法:
记录开始时间 startTime,然后每次回调时,获取当前时间 currentTime,计算差值 = currentTime – startTime,而获取当前时间
Android系统提供的两个方法:
SystemClock.uptimeMillis 和 System.currentTimeMillis
两个方法之间的区别:
SystemClock.uptimeMillis() // 从开机到现在的毫秒数(手机睡眠的时间不包括在内);
System.currentTimeMillis() // 从1970年1月1日 UTC到现在的毫秒数;
存在的问题:
System.currentTimeMillis() 获取的时间,是可以通过System.setCurrentTimeMillis修改的,那么,在某些情况下,一但被修改,时间间隔就不准了。
特别说明
AnimationUtils的解释中对这个问题进行了阐述:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * 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(); } |
转载请注明:方耀宇 - 个人技术博客 » Android时间间隔 SystemClock.uptimeMillis 和 System.currentTimeMillis