Android鬧鈴服務AlarmManager用法深入分析
本文實例講述了Android鬧鈴服務AlarmManager用法。分享給大家供大家參考,具體如下:
對應AlarmManage有一個AlarmManagerServie服務程 序,該服務程序才是正真提供鬧鈴服務的,它主要維護應用程序注冊下來的各類鬧鈴并適時的設置即將觸發(fā)的鬧鈴給鬧鈴設備(在系統(tǒng)中,linux實現(xiàn)的設備名 為"/dev/alarm"),并且一直監(jiān)聽鬧鈴設備,一旦有鬧鈴觸發(fā)或者是鬧鈴事件發(fā)生,AlarmManagerServie服務程序就會遍歷鬧鈴列 表找到相應的注冊鬧鈴并發(fā)出廣播。該服務程序在系統(tǒng)啟動時被系統(tǒng)服務程序system_service啟動并初始化鬧鈴設備(/dev/alarm)。當 然,在JAVA層的AlarmManagerService與Linux Alarm驅動程序接口之間還有一層封裝,那就是JNI。
AlarmManager將應用與服務分割開來后,使得應用程序開發(fā)者不用 關心具體的服務,而是直接通過AlarmManager來使用這種服務。這也許就是客戶/服務模式的好處吧。AlarmManager與 AlarmManagerServie之間是通過Binder來通信的,他們之間是多對一的關系。
在android系統(tǒng)中,AlarmManage提供了3個接口5種類型的鬧鈴服務。
3個接口:
// 取消已經(jīng)注冊的與參數(shù)匹配的鬧鈴 void cancel(PendingIntent operation) //注冊一個新的鬧鈴 void set( int type, long triggerAtTime, PendingIntent operation) //注冊一個重復類型的鬧鈴 void setRepeating( int type, long triggerAtTime, long interval, PendingIntent operation) //設置時區(qū) void setTimeZone(String timeZone)
Java代碼:
// 取消已經(jīng)注冊的與參數(shù)匹配的鬧鈴 void cancel(PendingIntent operation) //注冊一個新的鬧鈴 void set(int type, long triggerAtTime, PendingIntent operation) //注冊一個重復類型的鬧鈴 void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) //設置時區(qū) void setTimeZone(String timeZone)
5個鬧鈴類型
public static final int ELAPSED_REALTIME // 當系統(tǒng)進入睡眠狀態(tài)時,這種類型的鬧鈴不會喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時間是相對時間,是從系統(tǒng)啟動后開始計時的,包括睡眠時 間,可以通過調用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 //當系統(tǒng)進入睡眠狀態(tài)時,這種類型的鬧鈴不會喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時間是絕對時間,所用時間是UTC時間,可以通過調用 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),它是一種關機鬧鈴,就是說設備在關機狀態(tài)下也可以喚醒系統(tǒng),所以我們把它稱之為關機鬧鈴。使用方法同RTC類型,系統(tǒng)值為4(0x00000004)。
Java代碼 :
public static final int ELAPSED_REALTIME //當系統(tǒng)進入睡眠狀態(tài)時,這種類型的鬧鈴不會喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時間是相對時間,是從系統(tǒng)啟動后開始計時的,包括睡眠 時間,可以通過調用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 //當系統(tǒng)進入睡眠狀態(tài)時,這種類型的鬧鈴不會喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時間是絕對時間,所用時間是UTC時間,可以通過調用 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),它是一種關機鬧鈴,就是說設備在關機狀態(tài)下也可以喚醒系統(tǒng),所以我們把它稱之為關機鬧鈴。使用方法同RTC類型,系統(tǒng)值為 4(0x00000004)。
注意一個重要的參數(shù)PendingIntent。這個PendingIntent可以說是 Intent的進一步封裝,他既包含了Intent的描述又是Intent行為的執(zhí)行(這種定義也許不太嚴格),如果將Intent比作成一個訂單的 話,PendingIntent更像是一個下訂單的人,因為它既要負責將訂單發(fā)出去,也要負責訂單發(fā)送后的處理,比如發(fā)送成功后要準備驗收訂單貨物,發(fā)送 失敗后要重發(fā)還是取消訂單等操作。開發(fā)者可以通過調用
getActivity(Context, int, Intent, int)
getBroadcast(Context, int, Intent, int)
getService(Context, int, Intent, int)
三種不同方式來得到一個PendingIntent實例。
getBroadcast——通過該函數(shù)獲得的PendingIntent將會 扮演一個廣播的功能,就像調用 Context.sendBroadcast()函數(shù)一樣。當系統(tǒng)通過它要發(fā)送一個intent時要采用廣播的形式,并且在該intent中會包含相應的 intent接收對象,當然這個對象我們可以在創(chuàng)建PendingIntent的時候指定,也可以通過ACTION 和CATEGORY等描述讓系統(tǒng)自動找到該行為處理對象。
Intent intent = new Intent(AlarmController. this , OneShotAlarm. class ); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this , 0 , intent, 0 );
Java代碼:
Intent intent = new Intent(AlarmController.this, OneShotAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0);
getActivity——通過該函數(shù)獲得的PendingIntent可以直 接啟動新的activity, 就像調用 Context.startActivity(Intent)一樣.不過值得注意的是要想這個新的Activity不再是當前進程存在的Activity 時。我們在intent中必須使用Intent.FLAG_ACTIVITY_NEW_TASK.
// The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this , 0 , new Intent( this , AlarmService. class ), 0 );
Java代碼:
// The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, AlarmService.class), 0);
getService——通過該函數(shù)獲得的PengdingIntent可以直接啟動新的Service,就像調用Context.startService()一樣。
// 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 );
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android中Service實時向Activity傳遞數(shù)據(jù)實例分析
這篇文章主要介紹了Android中Service實時向Activity傳遞數(shù)據(jù)的方法,實例分析了Service組件基于線程操作實現(xiàn)數(shù)值實時傳遞的相關技巧,需要的朋友可以參考下2015-09-09Android筆記之:在ScrollView中嵌套ListView的方法
本篇文章是對Android中在ScrollView中嵌套ListView的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05