Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中的AlarmManager類

Android中的AlarmManager類

編輯:關於Android編程

一、聲明: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) and setExact(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 int RTC 該時間以System.currentTimeMillis()定義。鬧鐘不會喚醒設備。如果在系統休眠時鬧鐘觸發,它將不會被傳遞,直到下一次設備喚醒。常量值為1。

9、public static final int RTC_WAKEUP 該時間以System.currentTimeMillis()定義。當鬧鐘觸發時會喚醒設備。常量值為0。

五、方法:

1、public void cancel (PendingIntent operation) 根據匹配的Intent,移除鬧鐘。Intent封裝在operation中。任何類型的鬧鐘,只要匹配該條件都會被取消。

2、public void set (int type, long triggerAtMillis, PendingIntent operation) 設置鬧鐘。

注:對常用時間操作,使用Handler更容易,如果之前設置過同一個IntentSender的鬧鐘,那麼之前的首選會被取消。如果設置的時間在當前時間之前,那麼鬧鐘將馬上觸發。

參數:type:One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC, or RTC_WAKEUP.

3、public void setExact (int type, long triggerAtMillis, PendingIntent operation) 按照指定時間設置鬧鐘,類似於set(int, long, PendingIntent),但是該方法不允許操作系統調整觸發時間,鬧鐘會盡可能地在請求時間觸發。

注:只有在要求鬧鐘時間非常精確的情況才使用該方法進行設置。應用程序非常不支持在不必要的情況下使用該方法,因為它會降低操作系統減少電池使用的能力。

4、public void setInexactRepeating (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 after windowStartMillis. Note that this parameter is a duration, not the timestamp of the end of the window.

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved