首頁 收藏 QQ群
 網(wǎng)站導(dǎo)航

ZNDS智能電視網(wǎng) 推薦當(dāng)貝市場

TV應(yīng)用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計算器

綜合交流 / 評測 / 活動區(qū)

交流區(qū) | 測硬件 | 網(wǎng)站活動 | Z幣中心

新手入門 / 進(jìn)階 / 社區(qū)互助

新手 | 你問我答 | 免費(fèi)刷機(jī)救磚 | ROM固件

查看: 13275|回復(fù): 0
上一主題 下一主題
[教程]

深入學(xué)習(xí)android之AlarmManager

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:30 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
5對應(yīng)AlarmManage有一個AlarmManagerServie服務(wù)程序,該服務(wù)程序才是正真提供鬧鈴服務(wù)的,它主要維護(hù)應(yīng)用程序注冊下來的各類鬧鈴并適時的設(shè)置即將觸發(fā)的鬧鈴給鬧鈴設(shè)備(在系統(tǒng)中,linux實(shí)現(xiàn)的設(shè)備名為”/dev/alarm”),并且一直監(jiān)聽鬧鈴設(shè)備,一旦有鬧鈴觸發(fā)或者是鬧鈴事件發(fā)生,AlarmManagerServie服務(wù)程序就會遍歷鬧鈴列表找到相應(yīng)的注冊鬧鈴并發(fā)出廣播。該服務(wù)程序在系統(tǒng)啟動時被系統(tǒng)服務(wù)程序system_service啟動并初始化鬧鈴設(shè)備(/dev/alarm)。當(dāng)然,在JAVA層的AlarmManagerService與Linux Alarm驅(qū)動程序接口之間還有一層封裝,那就是JNI。   
   
  AlarmManager將應(yīng)用與服務(wù)分割開來后,使得應(yīng)用程序開發(fā)者不用關(guān)心具體的服務(wù),而是直接通過AlarmManager來使用這種服務(wù)。這也許就是客戶/服務(wù)模式的好處吧。AlarmManager與 AlarmManagerServie之間是通過Binder來通信的,他們之間是多對一的關(guān)系。   
  在android系統(tǒng)中,AlarmManage提供了3個接口5種類型的鬧鈴服務(wù)。   
  3個接口:   
  1. // 取消已經(jīng)注冊的與參數(shù)匹配的鬧鈴   
       
      void    cancel(PendingIntent operation)   
       
      //注冊一個新的鬧鈴   
       
      void    set( int  type,  long  triggerAtTime, PendingIntent operation)   
       
      //注冊一個重復(fù)類型的鬧鈴   
       
      void    setRepeating( int  type,  long  triggerAtTime,  long  interval, PendingIntent operation)   
       
      //設(shè)置時區(qū)   
       
      void    setTimeZone(String timeZone)
復(fù)制代碼
Java代碼   
  1.  // 取消已經(jīng)注冊的與參數(shù)匹配的鬧鈴   
       
      void   cancel(PendingIntent operation)   
       
      //注冊一個新的鬧鈴   
       
      void   set(int type, long triggerAtTime, PendingIntent operation)   
       
      //注冊一個重復(fù)類型的鬧鈴   
       
      void   setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)   
       
      //設(shè)置時區(qū)   
       
      void   setTimeZone(String timeZone)
復(fù)制代碼
  
  1.  public   static   final   int  ELAPSED_REALTIME   
       
      // 當(dāng)系統(tǒng)進(jìn)入睡眠狀態(tài)時,這種類型的鬧鈴不會喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時間是相對時間,是從系統(tǒng)啟動后開始計時的,包括睡眠時間,可以通過調(diào)用SystemClock.elapsedRealtime()獲得。系統(tǒng)值是3    (0x00000003)。   
       
      public   static   final   int  ELAPSED_REALTIME_WAKEUP   
       
      //能喚醒系統(tǒng),用法同ELAPSED_REALTIME,系統(tǒng)值是2 (0x00000002) 。   
       
      public   static   final   int  RTC   
       
      //當(dāng)系統(tǒng)進(jìn)入睡眠狀態(tài)時,這種類型的鬧鈴不會喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時間是絕對時間,所用時間是UTC時間,可以通過調(diào)用 System.currentTimeMillis()獲得。系統(tǒng)值是1 (0x00000001) 。   
       
      public   static   final   int  RTC_WAKEUP   
       
      //能喚醒系統(tǒng),用法同RTC類型,系統(tǒng)值為 0 (0x00000000) 。   
       
      Public static   final   int  POWER_OFF_WAKEUP   
       
      //能喚醒系統(tǒng),它是一種關(guān)機(jī)鬧鈴,就是說設(shè)備在關(guān)機(jī)狀態(tài)下也可以喚醒系統(tǒng),所以我們把它稱之為關(guān)機(jī)鬧鈴。使用方法同RTC類型,系統(tǒng)值為4(0x00000004)。
復(fù)制代碼
注意一個重要的參數(shù)PendingIntent。這個PendingIntent可以說是 Intent的進(jìn)一步封裝,他既包含了Intent的描述又是Intent行為的執(zhí)行(這種定義也許不太嚴(yán)格),如果將Intent比作成一個訂單的話,PendingIntent更像是一個下訂單的人,因?yàn)樗纫?fù)責(zé)將訂單發(fā)出去,也要負(fù)責(zé)訂單發(fā)送后的處理,比如發(fā)送成功后要準(zhǔn)備驗(yàn)收訂單貨物,發(fā)送失敗后要重發(fā)還是取消訂單等操作。開發(fā)者可以通過調(diào)用getActivity(Context, int, Intent, int)   
   
  getBroadcast(Context, int, Intent, int)   
  getService(Context, int, Intent, int)   
   
  三種不同方式來得到一個PendingIntent實(shí)例。   
   
  getBroadcast——通過該函數(shù)獲得的PendingIntent將會扮演一個廣播的功能,就像調(diào)用 Context。sendBroadcast()函數(shù)一樣。當(dāng)系統(tǒng)通過它要發(fā)送一個intent時要采用廣播的形式,并且在該intent中會包含相應(yīng)的 intent接收對象,當(dāng)然這個對象我們可以在創(chuàng)建PendingIntent的時候指定,也可以通過ACTION 和CATEGORY等描述讓系統(tǒng)自動找到該行為處理對象。   
  1. Intent intent =  new  Intent(AlarmController. this , OneShotAlarm. class );   
       
    PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this ,  0 , intent,  0 );
復(fù)制代碼
Java代碼   
  1. Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);   
       
    PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0);
復(fù)制代碼
getActivity——通過該函數(shù)獲得的PendingIntent可以直接啟動新的activity, 就像調(diào)用 Context.startActivity(Intent)一樣。不過值得注意的是要想這個新的Activity不再是當(dāng)前進(jìn)程存在的Activity 時。我們在intent中必須使用Intent.FLAG_ACTIVITY_NEW_TASK。   
  1. // The PendingIntent to launch our activity if the user selects this notification   
       
    PendingIntent contentIntent = PendingIntent.getActivity(this ,  0 ,   new  Intent( this , AlarmService. class ),  0 );
復(fù)制代碼
Java代碼   
  1. // The PendingIntent to launch our activity if the user selects this notification   
       
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  new Intent(this, AlarmService.class), 0);
復(fù)制代碼
getService——通過該函數(shù)獲得的PengdingIntent可以直接啟動新的Service,就像調(diào)用Context.startService()一樣。   
  1.     view plain copy to clipboard print ?   
       
      // Create an IntentSender that will launch our service, to be scheduled   
       
      // with the alarm manager.   
       
      mAlarmSender = PendingIntent.getService(AlarmService.this ,   
       
      0 ,  new  Intent(AlarmService.this , AlarmService_Service. class ),  0 );   
復(fù)制代碼
</div

上一篇:AlarmManager的使用
下一篇:Android源碼下載
您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

Archiver|新帖|標(biāo)簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號 )

網(wǎng)絡(luò)信息服務(wù)信用承諾書 | 增值電信業(yè)務(wù)經(jīng)營許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號

GMT+8, 2024-10-19 22:42 , Processed in 0.057365 second(s), 15 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報:report#znds.com (請將#替換為@)

© 2007-2024 ZNDS.Com

快速回復(fù) 返回頂部 返回列表