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

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

SystemClock.uptimeMillis 和 System.currentTimeMillis

这两种方法有何区别呢?

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

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

但是,第2个时间,是可以通过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
     */

想想看,假如用第2个时间来做动画,万一被其它应用在动画过程中修改了时间,呃,这个我是无法想像的惊恐

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值