Android.os.SystemClock 介紹

原文出處: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.
    該時間是基於世界時間的,它返回的是從January 1, 1970 00:00:00 UTC到現在時間已經逝去了多多少millisecond,當我設置Android手機的系統時間時,會應該影響該值。
  • 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.
    它表示的是手機從啓動到現在的運行時間,且不包括系統sleep(CPU關閉)的時間,很多系統的內部時間都是基於此,比如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.
    它表示的是手機從啓動到現在的運行時間,且包括系統sleep(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 withThread.interrupt(), and you must handle InterruptedException.
    標準的方法像Thread.sleep(millis) 和 Object.wait(millis)總是可用的,這些方法使用的是uptimeMillis()時鐘,如果設備進入深度休眠,剩餘的時間將被推遲直到系統喚醒。這些同步方法可能被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)是一個類似於Thread.sleep(millis)的實用方法,但是它忽略InterruptedException異常。使用該函數產生的延遲如果你不使用Thread.interrupt(),因爲它會保存線程的中斷狀態。
  • 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可以在一個相對或者絕對的時間設置異步回調,Handler類對象也使用uptimeMillis()時鐘,而且需要一個loop(經常出現在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可以觸發一次或重複事件,即使設備深度休眠或者應用程序沒有運行。事件可以選擇用 currentTimeMillis或者elapsedRealtime()(ELAPSED_REALTIME)來設置時間,當事件發生會觸發一個廣播。

常用函數

return methods
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.

註釋:

1、public static long currentThreadTimeMillis () 返在當前線程運行的毫秒數。

2、public static long elapsedRealtime () 返回系統啓動到現在的毫秒數,包含休眠時間。

3、public static long elapsedRealtimeNanos () 返回系統啓動到現在的納秒數,包含休眠時間。

4、public static boolean setCurrentTimeMillis (long millis) 設置當前wall time,要求調用進程有許可權限。返回是否成功。

5、public static void sleep (long ms) 等待給定的時間。和Thread.sleep(millis)類似,但是它不會拋出InterruptedException異常。事件被推遲到下一個中斷操作。該方法直到指定的時間過去才返回。

6、public static long uptimeMillis () 返回系統啓動到現在的毫秒數,不包含休眠時間。就是說統計系統啓動到現在的非休眠期時間。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章