原文出處:http://www.ccbu.cc/android/android-systemclock
Class Overview
Core timekeeping facilities.
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 andACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes.
該時(shí)間是基于世界時(shí)間的,它返回的是從January 1, 1970 00:00:00 UTC到現(xiàn)在時(shí)間已經(jīng)逝去了多多少millisecond,當(dāng)我設(shè)置Android手機(jī)的系統(tǒng)時(shí)間時(shí),會(huì)應(yīng)該影響該值。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 asThread.sleep(millls), Object.wait(millis), and System.nanoTime(). This clock is guaranteed to be monotonic, and is the recommended basis for the general purpose interval timing of user interface events, performance measurements, and anything else that does not need to measure elapsed time during device sleep. Most methods that accept a timestamp value expect the uptimeMillis() clock.
它表示的是手機(jī)從啟動(dòng)到現(xiàn)在的運(yùn)行時(shí)間,且不包括系統(tǒng)sleep(CPU關(guān)閉)的時(shí)間,很多系統(tǒng)的內(nèi)部時(shí)間都是基于此,比如Thread.sleep(millls), Object.wait(millis), and System.nanoTime()elapsedRealtime() is counted in milliseconds since the system was booted, including deep sleep. This clock should be used when measuring time intervals that may span periods of system sleep.
它表示的是手機(jī)從啟動(dòng)到現(xiàn)在的運(yùn)行時(shí)間,且包括系統(tǒng)sleep(CPU關(guān)閉)的時(shí)間
There are several mechanisms for controlling the timing of events(有幾種機(jī)制控制事件發(fā)生的時(shí)間):
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 withThread.interrupt(), and you must handle InterruptedException.
標(biāo)準(zhǔn)的方法像Thread.sleep(millis) 和 Object.wait(millis)總是可用的,這些方法使用的是uptimeMillis()時(shí)鐘,如果設(shè)備進(jìn)入深度休眠,剩余的時(shí)間將被推遲直到系統(tǒng)喚醒。這些同步方法可能被Thread.interrupt()中斷,并且你必須處理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.
SystemClock.sleep(millis)是一個(gè)類(lèi)似于Thread.sleep(millis)的實(shí)用方法,但是它忽略InterruptedException異常。使用該函數(shù)產(chǎn)生的延遲如果你不使用Thread.interrupt(),因?yàn)樗鼤?huì)保存線程的中斷狀態(tài)。The Handler class can schedule asynchronous callbacks at an absolute or relative time. Handler objects also use the uptimeMillis() clock, and require anevent loop (normally present in any GUI application).
Handler可以在一個(gè)相對(duì)或者絕對(duì)的時(shí)間設(shè)置異步回調(diào),Handler類(lèi)對(duì)象也使用uptimeMillis()時(shí)鐘,而且需要一個(gè)loop(經(jīng)常出現(xiàn)在GUI程序中)。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.
AlarmManager可以觸發(fā)一次或重復(fù)事件,即使設(shè)備深度休眠或者應(yīng)用程序沒(méi)有運(yùn)行。事件可以選擇用 currentTimeMillis或者elapsedRealtime()(ELAPSED_REALTIME)來(lái)設(shè)置時(shí)間,當(dāng)事件發(fā)生會(huì)觸發(fā)一個(gè)廣播。
常用函數(shù)
| return | methods | description |
|---|---|---|
| static long | currentThreadTimeMillis() | Returns milliseconds running in the current thread. |
| static long | elapsedRealtime() | Returns milliseconds since boot, including time spent in sleep. |
| static long | elapsedRealtimeNanos() | Returns nanoseconds since boot, including time spent in sleep. |
| static boolean | setCurrentTimeMillis(long millis) | Sets the current wall time, in milliseconds. |
| static void | sleep(long ms) | Waits a given number of milliseconds (of uptimeMillis) before returning. |
| static long | uptimeMillis() | Returns milliseconds since boot, not counting time spent in deep sleep. |
注釋?zhuān)?/strong>
1、public static long currentThreadTimeMillis () 返在當(dāng)前線程運(yùn)行的毫秒數(shù)。
2、public static long elapsedRealtime () 返回系統(tǒng)啟動(dòng)到現(xiàn)在的毫秒數(shù),包含休眠時(shí)間。
3、public static long elapsedRealtimeNanos () 返回系統(tǒng)啟動(dòng)到現(xiàn)在的納秒數(shù),包含休眠時(shí)間。
4、public static boolean setCurrentTimeMillis (long millis) 設(shè)置當(dāng)前wall time,要求調(diào)用進(jìn)程有許可權(quán)限。返回是否成功。
5、public static void sleep (long ms) 等待給定的時(shí)間。和Thread.sleep(millis)類(lèi)似,但是它不會(huì)拋出InterruptedException異常。事件被推遲到下一個(gè)中斷操作。該方法直到指定的時(shí)間過(guò)去才返回。
6、public static long uptimeMillis () 返回系統(tǒng)啟動(dòng)到現(xiàn)在的毫秒數(shù),不包含休眠時(shí)間。就是說(shuō)統(tǒng)計(jì)系統(tǒng)啟動(dòng)到現(xiàn)在的非休眠期時(shí)間。