SystemClock API doc,核心计时设备分析

 

Three different clocks are available, and they should not be confused:

有三种不同的时钟不应该混淆:

 System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis(), consider listening to the ACTION_TIME_TICK, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes.

 

 uptimeMillis() is counted in milliseconds since the system was booted. This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as Thread.sleep(millls), Object.wait(millis), and System.nanoTime(). This clock is guaranteed to be monotonic(单调的,我的理解是类似于单调递增函数), and is suitable for interval timing when the interval does not span device sleep. Most methods that accept a timestamp value currently expect the uptimeMillis() clock.

 

 elapsedRealtime() and elapsedRealtimeNanos() return the time since the system was booted, and include deep sleep. This clock is guaranteed to be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend basis for general purpose interval timing.

System.currentTimeMillis() 是自 1970年1月1日开始至调用时的毫秒数。 只有在与实际日期和时间对应很重要时,例如在日历或闹钟应用程序中,才应使用此时钟。 间隔或经过时间测量应使用不同的时钟。并考虑监听ACTION_TIME_TICK,ACTION_TIME_CHANGED和ACTION_TIMEZONE_CHANGED Intent广播以查明时间的变化。

 

uptimeMillis() 是最近一次手机开机以来至调用时的毫秒数,当系统进入深度睡眠状态(CPU关闭,显示屛熄灭,设备等待外部输入)时,此时钟停止,但不受时钟缩放,空闲或其他省电机制的影响。 这是大多数间隔时间的基础,例如Thread.sleep(millls),Object.wait(millis)和System.nanoTime()。

 

③ elapsedRealtime() and elapsedRealtimeNanos()  是最近一次手机开机以来至调用时的时间,并包括深度睡眠。 该时钟保证是单调的,即使在CPU处于省电模式时也会继续计时,因此是通用间隔时序的推荐基础。

 


 

There are several mechanisms for controlling the timing of events:

这里有几种机制可以控制事件的时间:

Standard functions like Thread.sleep(millis) and Object.wait(millis) are always available. These functions use the uptimeMillis() clock; if the device enters sleep, the remainder of the time will be postponed until the device wakes up. These synchronous functions may be interrupted with Thread.interrupt(), and you must handle InterruptedException.

 

SystemClock.sleep(millis) is a utility function very similar to Thread.sleep(millis), but it ignores InterruptedException. Use this function for delays if you do not use Thread.interrupt(), as it will preserve the interrupted state of the thread.

 

The Handler class can schedule asynchronous callbacks at an absolute or relative time. Handler objects also use the uptimeMillis() clock, and require an event loop (normally present in any GUI application).

 

The AlarmManager can trigger one-time or recurring events which occur even when the device is in deep sleep or your application is not running. Events may be scheduled with your choice of currentTimeMillis() (RTC) or elapsedRealtime() (ELAPSED_REALTIME), and cause an Intent broadcast when they occur.

标准函数比如Thread.sleep(millis)和Object.wait(millis)等,开发者随时都可以调用。 这些使用uptimeMillis()时钟; 如果设备进入睡眠状态,则剩余的时间将被推迟,直到设备唤醒。 这些同步函数可能会被Thread.interrupt()中断,所以必须处理InterruptedException

 

 SystemClock.sleep(millis)是一个非常类似于Thread.sleep(millis)的函数,但它忽略了InterruptedException。 

 

 Handler类可以在绝对或相对时间安排异步回调。Handler对象也使用uptimeMillis() 时钟

 

 AlarmManager可以触发即使设备处于深度睡眠状态或应用程序未运行时发生的一次性或重复性事件。开发者可以 选择使用 currentTimeMillis()(RTC)或elapsedRealtime()(ELAPSED_REALTIME)来安排事件,并在发生时进行Intent广播。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值