一、聲明:public class AlarmManager extends Object
二、類結構:
| java.lang.Object |
| ? |
android.app.AlarmManager |
三、概述:該類提供一種接近系統鬧鐘服務的方式,允許你去設定一個將來的時間點去執行你的應用程式。當你的鬧鐘響起(時間到)時,在它上面註冊的一個意圖(Intent)將會被系統以廣播發出,然後自動啟動目標程式,如果它並沒有正在運行。註冊的鬧鐘會被保留即使裝置處於休眠中(如果鬧鐘在給定時間響起可以選擇是否喚醒裝置)。如果鬧鐘關閉或者重啟,鬧鐘將被清除。
只要廣播的onReceive()方法正在執行,這鬧鐘管理者(AlarmManager)會持有一個CPU喚醒鎖,這是為了保證手機不會休眠直到處理完該廣播,一旦onReceive()返回,那麼鬧鐘管理者將會釋放喚醒鎖。這意味著你的手機在廣播一處理完有可能進入休眠,如果你的鬧鐘廣播接收者調用的是Context.startService(),那麼手機有可能在被請求的服務執行之前進入休眠,為了防止這種情況,你的BroadcastReceiver和服務需要實現一個單獨的喚醒鎖策略以確保手機繼續運行,直到服務可用。
註:該類適用於你想讓應用程式在將來某個指定時間點執行的情況,即使你的應用程式現在沒有運行。對一般的時間操作,使用Handler是更容易和更有效率的。
自API 19以後,鬧鐘觸發是不精確的:作業系統將會使用鬧鐘時間產生變化,為了減少喚醒和電池使用。有新的API支援那些需要嚴格鬧鐘觸發時間的應用程式,參見:setWindow(int, long, long, PendingIntent) andsetExact(int, long, PendingIntent).而在API 19以前的版本參照以前的處理方法,他們都是按照請求精確觸發的。
四、常量:
1、public static final int ELAPSED_REALTIME 該鬧鐘時間以SystemClock.elapsedRealtime()定義。鬧鐘不會喚醒裝置。如果在系統休眠時鬧鐘觸發,它將不會被傳遞,直到下一次裝置喚醒。常量值為3。
2、public static final int ELAPSED_REALTIME_WAKEUP 該鬧鐘時間以SystemClock.elapsedRealtime()定義。當鬧鐘觸發時會喚醒裝置。常量值為2。
3、public static final long INTERVAL_DAY 可用的不精確的複發間隔。通過setInexactRepeating(int, long, long, PendingIntent)去辨別。運行在API 19或者之前。常量值:86400000
4、public static final long INTERVAL_FIFTEEN_MINUTES 可用的不精確的複發間隔。通過setInexactRepeating(int, long, long, PendingIntent)去辨別。運行在API 19或者之前。常量值:900000
5、public static final long INTERVAL_HALF_DAY 可用的不精確的複發間隔。通過setInexactRepeating(int, long, long, PendingIntent)去辨別。運行在API 19或者之前。常量值: 43200000
6、public static final long INTERVAL_HALF_HOUR 可用的不精確的複發間隔。通過setInexactRepeating(int, long, long, PendingIntent)去辨別。運行在API 19或者之前。常量值: 1800000
7、public static final long INTERVAL_HOUR 可用的不精確的複發間隔。通過setInexactRepeating(int, long, long, PendingIntent)去辨別。運行在API 19或者之前。常量值: 3600000
8、public static final intRTC 該時間以System.currentTimeMillis()定義。鬧鐘不會喚醒裝置。如果在系統休眠時鬧鐘觸發,它將不會被傳遞,直到下一次裝置喚醒。常量值為1。
9、public static final intRTC_WAKEUP 該時間以System.currentTimeMillis()定義。當鬧鐘觸發時會喚醒裝置。常量值為0。
五、方法:
1、public voidcancel (PendingIntent operation) 根據匹配的Intent,移除鬧鐘。Intent封裝在operation中。任何類型的鬧鐘,只要匹配該條件都會被取消。
2、public voidset (int type, long triggerAtMillis, PendingIntent operation) 設定鬧鐘。
註:對常用時間操作,使用Handler更容易,如果之前設定過同一個IntentSender的鬧鐘,那麼之前的首選會被取消。如果設定的時間在目前時間之前,那麼鬧鐘將馬上觸發。
參數:type:One ofELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC, orRTC_WAKEUP.
3、public voidsetExact (int type, long triggerAtMillis, PendingIntent operation) 按照指定時間設定鬧鐘,類似於set(int, long, PendingIntent),但是該方法不允許作業系統調整觸發時間,鬧鐘會儘可能地在請求時間觸發。
註:只有在要求鬧鐘時間非常精確的情況才使用該方法進行設定。應用程式非常不支援在不必要的情況下使用該方法,因為它會降低作業系統減少電池使用的能力。
4、public voidsetInexactRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
設定一個重複的鬧鐘,它的觸發時間是不精確的。該方法比setRepeating(int, long, long, PendingIntent)節省電量。因為系統能調整觸發時間,避免不必要的喚醒裝置。
5、public void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) 設定重複鬧鐘。
6、public void setTime(long millis) 設定系統“牆”時鐘。需要android.permission.SET_TIME.許可權。
7、public void setTimeZone(String timeZone) 設定系統預設時區,要求android.permission.SET_TIME_ZONE.許可權。
參數:由TimeZone支援。
8、public void setWindow(int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation) 設定一個鬧鐘在給定的時間窗觸發。類似於set(int, long, PendingIntent),該方法允許應用程式精確地控制作業系統調整鬧鐘觸發時間的程度。
參數:
| windowStartMillis |
The earliest time, in milliseconds, that the alarm should be delivered, expressed in the appropriate clock's units (depending on the alarm type). |
| windowLengthMillis |
The length of the requested delivery window, in milliseconds. The alarm will be delivered no later than this many milliseconds afterwindowStartMillis. Note that this parameter is a duration, not the timestamp of the end of the window. |