Android时间间隔 SystemClock.uptimeMillis 和 System.currentTimeMillis

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();
    }
  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值