深入學習android之AlarmManager

對應AlarmManage有一個AlarmManagerServie服務程序,該服務程序纔是正真提供鬧鈴服務的,它主要維護應用程序註冊下來的各類鬧鈴並適時的設置即將觸發的鬧鈴給鬧鈴設備(在系統中,linux實現的設備名爲”/dev/alarm”),並且一直監聽鬧鈴設備,一旦有鬧鈴觸發或者是鬧鈴事件發生,AlarmManagerServie服務程序就會遍歷鬧鈴列表找到相應的註冊鬧鈴併發出廣播。該服務程序在系統啓動時被系統服務程序system_service啓動並初始化鬧鈴設備(/dev/alarm)。當然,在JAVA層的AlarmManagerService與Linux Alarm驅動程序接口之間還有一層封裝,那就是JNI。

  AlarmManager將應用與服務分割開來後,使得應用程序開發者不用關心具體的服務,而是直接通過AlarmManager來使用這種服務。這也許就是客戶/服務模式的好處吧。AlarmManager與 AlarmManagerServie之間是通過Binder來通信的,他們之間是多對一的關係。

     在android系統中,AlarmManage提供了3個接口5種類型的鬧鈴服務。

3個接口:

  1. // 取消已經註冊的與參數匹配的鬧鈴    
  2. void    cancel(PendingIntent operation)  
  3.   
  4.   //註冊一個新的鬧鈴   
  5. void    set( int  type,  long  triggerAtTime, PendingIntent operation)  
  6.   
  7.   //註冊一個重複類型的鬧鈴   
  8. void    setRepeating( int  type,  long  triggerAtTime,  long  interval, PendingIntent operation)  
  9.   
  10.     //設置時區   
  11. void    setTimeZone(String timeZone)  
Java代碼 複製代碼
  1. // 取消已經註冊的與參數匹配的鬧鈴    
  2. void   cancel(PendingIntent operation)   
  3.   //註冊一個新的鬧鈴   
  4. void   set(int type, long triggerAtTime, PendingIntent operation)   
  5.   //註冊一個重複類型的鬧鈴   
  6. void   setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)   
  7.     //設置時區   
  8. void   setTimeZone(String timeZone)  

5個鬧鈴類型

  1. public   static   final   int  ELAPSED_REALTIME  
  2.         // 當系統進入睡眠狀態時,這種類型的鬧鈴不會喚醒系統。直到系統下次被喚醒才傳遞它,該鬧鈴所用的時間是相對時間,是從系統啓動後開始計時的,包括睡眠時間,可以通過調用SystemClock.elapsedRealtime()獲得。系統值是3    (0x00000003)。   
  3.   
  4.         public   static   final   int  ELAPSED_REALTIME_WAKEUP  
  5.         //能喚醒系統,用法同ELAPSED_REALTIME,系統值是2 (0x00000002) 。   
  6.   
  7.         public   static   final   int  RTC  
  8.         //當系統進入睡眠狀態時,這種類型的鬧鈴不會喚醒系統。直到系統下次被喚醒才傳遞它,該鬧鈴所用的時間是絕對時間,所用時間是UTC時間,可以通過調用 System.currentTimeMillis()獲得。系統值是1 (0x00000001) 。   
  9.   
  10.         public   static   final   int  RTC_WAKEUP  
  11.         //能喚醒系統,用法同RTC類型,系統值爲 0 (0x00000000) 。   
  12.   
  13.         Public static   final   int  POWER_OFF_WAKEUP  
  14.         //能喚醒系統,它是一種關機鬧鈴,就是說設備在關機狀態下也可以喚醒系統,所以我們把它稱之爲關機鬧鈴。使用方法同RTC類型,系統值爲4(0x00000004)。   
Java代碼 複製代碼
  1. public  
  2.  static final int ELAPSED_REALTIME   
  3.            
  4. //當系統進入睡眠狀態時,這種類型的鬧鈴不會喚醒系統。直到系統下次被喚醒才傳遞它,該鬧鈴所用的時間是相對時間,是從系統啓動後開始計時的,包括睡眠   
  5. 時間,可以通過調用SystemClock.elapsedRealtime()獲得。系統值是3    (0x00000003)。   
  6.         public static final int ELAPSED_REALTIME_WAKEUP   
  7.         //能喚醒系統,用法同ELAPSED_REALTIME,系統值是2 (0x00000002) 。   
  8.         public static final int RTC   
  9.            
  10. //當系統進入睡眠狀態時,這種類型的鬧鈴不會喚醒系統。直到系統下次被喚醒才傳遞它,該鬧鈴所用的時間是絕對時間,所用時間是UTC時間,可以通過調用   
  11.  System.currentTimeMillis()獲得。系統值是1 (0x00000001) 。   
  12.         public static final int RTC_WAKEUP   
  13.         //能喚醒系統,用法同RTC類型,系統值爲 0 (0x00000000) 。   
  14.         Public static final int POWER_OFF_WAKEUP   
  15.            
  16. //能喚醒系統,它是一種關機鬧鈴,就是說設備在關機狀態下也可以喚醒系統,所以我們把它稱之爲關機鬧鈴。使用方法同RTC類型,系統值爲   
  17. 4(0x00000004)。  
  

      注意一個重要的參數PendingIntent。這個PendingIntent可以說是 Intent的進一步封裝,他既包含了Intent的描述又是Intent行爲的執行(這種定義也許不太嚴格),如果將Intent比作成一個訂單的話,PendingIntent更像是一個下訂單的人,因爲它既要負責將訂單發出去,也要負責訂單發送後的處理,比如發送成功後要準備驗收訂單貨物,發送失敗後要重發還是取消訂單等操作。開發者可以通過調用getActivity(Context, int, Intent, int)

getBroadcast(Context, int, Intent, int)

getService(Context, int, Intent, int)

三種不同方式來得到一個PendingIntent實例。

getBroadcast——通過該函數獲得的PendingIntent將會扮演一個廣播的功能,就像調用 Context.sendBroadcast()函數一樣。當系統通過它要發送一個intent時要採用廣播的形式,並且在該intent中會包含相應的 intent接收對象,當然這個對象我們可以在創建PendingIntent的時候指定,也可以通過ACTION 和CATEGORY等描述讓系統自動找到該行爲處理對象。

  1. Intent intent =  new  Intent(AlarmController. this , OneShotAlarm. class );  
  2. PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this 0 , intent,  0 );  
Java代碼 複製代碼
  1. Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);   
  2. PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this0, intent, 0);  

getActivity——通過該函數獲得的PendingIntent可以直接啓動新的activity, 就像調用 Context.startActivity(Intent)一樣.不過值得注意的是要想這個新的Activity不再是當前進程存在的Activity 時。我們在intent中必須使用Intent.FLAG_ACTIVITY_NEW_TASK.

  1. // The PendingIntent to launch our activity if the user selects this notification   
  2. PendingIntent contentIntent = PendingIntent.getActivity(this 0 ,   new  Intent( this , AlarmService. class ),  0 );  
Java代碼 複製代碼
  1. // The PendingIntent to launch our activity if the user selects this notification   
  2. PendingIntent contentIntent = PendingIntent.getActivity(this0,  new Intent(this, AlarmService.class), 0);  

getService——通過該函數獲得的PengdingIntent可以直接啓動新的Service,就像調用Context.startService()一樣。

  1. // Create an IntentSender that will launch our service, to be scheduled   
  2.     // with the alarm manager.   
  3.     mAlarmSender = PendingIntent.getService(AlarmService.this ,  
  4.                 0 new  Intent(AlarmService. this , AlarmService_Service. class ),  0 ); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章