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

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

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

軟件下載 | 游戲 | 討論 | 電視計(jì)算器

綜合交流 / 評(píng)測(cè) / 活動(dòng)區(qū)

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

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

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

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

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

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

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

GMT+8, 2025-2-9 09:45 , Processed in 0.062929 second(s), 15 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報(bào):report#znds.com (請(qǐng)將#替換為@)

© 2007-2025 ZNDS.Com

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