Android 編程下 AlarmManager

對應 AlarmManager 有一個 AlarmManagerServie 服務程序,該服務程序才是正真提供鬧鈴服務的,它主要維護應用程序注冊的各類鬧鈴并適時的設置即將觸發的鬧鈴給鬧鈴設備 ( 在系統中,Linux 實現的設備名為 ”/dev/alarm” ) ,并且一直監聽鬧鈴設備,一旦有鬧鈴觸發或者是鬧鈴事件發生,AlarmManagerServie 服務程序就會遍歷鬧鈴列表找到相應的注冊鬧鈴并發出廣播。該服務程序在系統啟動時被系統服務程序 system_service 啟動并初始化鬧鈴設備 ( /dev/alarm ) 。當然,在 JAVA 層的 AlarmManagerService 與 Linux Alarm 驅動程序接口之間還有一層封裝,那就是 JNI。(參考官方文檔:AlarmManager | Android Developers)

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

AlarmManager 提供了 8 個與之相關的方法:

? AlarmManager Public Methods (方法)

  void  

  cancel(PendingIntent operation)

  取消參數匹配的鬧鈴

void

  set(int type, long triggerAtMillis, PendingIntent operation)

  注冊一個新的鬧鈴

void

  setExact(int type, long triggerAtMillis, PendingIntent operation)

  注冊一個新的鬧鈴,這個鬧鈴將在指定的時間被準確的執行

void

  setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

  注冊一個對觸發時間并不是很精準的鬧鈴,例如,一個鬧鈴每小時都會重復,但不一定都是在每個小時的最開始被觸發

void

  setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

  注冊一個重復類型的鬧鈴

void

  setTime(long millis)

  設定系統時鐘時間

void

  setTimeZone(String timeZone)

  設置系統默認時區

void

  setWindow(int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation)

  注冊一個鬧鈴,這個鬧鈴將在給定的時間窗口內被觸發

?

? Constants (類型)

  public static final int ELAPSED_REALTIME

  Constant Value: 3 (0x00000003)

  這種類型的鬧鈴不會喚醒系統,如果這種鬧鈴在系統休眠狀態終止,它會在系統下次喚醒的時候被觸發。該種鬧鈴所用的時間是相對時間,是從系統啟動后開始

  計時的,包括睡眠時間,可以通過調用 SystemClock.elapsedRealtime() 獲得。

  public static final int ELAPSED_REALTIME_WAKEUP

  Constant Value: 2 (0x00000002)

  這種類型的鬧鈴能在終止的時候喚醒系統,用法同 ELAPSED_REALTIME

  public static final long INTERVAL_DAY

  Constant Value: 86400000 (0x0000000005265c00)

  public static final long INTERVAL_FIFTEEN_MINUTES

  Constant Value: 900000 (0x00000000000dbba0)

  public static final long INTERVAL_HALF_DAY

  Constant Value: 43200000 (0x0000000002932e00)

  public static final long INTERVAL_HALF_HOUR

  Constant Value: 1800000 (0x00000000001b7740)

  public static final long INTERVAL_HOUR

  Constant Value: 3600000 (0x000000000036ee80)

  public static final int RTC

  Constant Value: 1 (0x00000001)

  這種類型的鬧鈴不會喚醒系統,如果這種鬧鈴在系統休眠狀態終止,它會在系統下次喚醒的時候被觸發。該鬧鈴所用的時間是絕對時間,可以通過調用

  System.currentTimeMillis() (wall clock time in UTC) 獲得。

  public static final int RTC_WAKEUP

  Constant Value: 0 (0x00000000)

  這種類型的鬧鈴能在終止的時候喚醒系統,用法同 RTC

?

注意一個重要的參數 PendingIntent。這個 PendingIntent 可以說是 Intent 的進一步封裝, 因為它不僅對 Intent 進行了描述,也對使用該 Intent 所要完成的目標動作進行了描述。開發者可以通過以下六個方法來獲得 PendingIntent 實例。(參考官方文檔:PendingIntent | Android Developers)

?

? PendingIntent Public Methods (方法)

  static PendingIntent  

  getActivities(Context context, int requestCode, Intent[] intents, int flags)

  方法同 getActivity(Context, int, Intent, int),但允許提交一個由多個 Intent 組成的數組

static PendingIntent

  getActivities(Context context, int requestCode, Intent[] intents, int flags, Bundle options)

  方法同 getActivity(Context, int, Intent, int),但允許提交一個由多個 Intent 組成的數組

static PendingIntent

  getActivity(Context context, int requestCode, Intent intent, int flags)

  返回一個可以打開新 Activity 的 PendingIntent,相當于調用 ?Context.startActivity(Intent)。

static PendingIntent

  getActivity(Context context, int requestCode, Intent intent, int flags, Bundle options)

  返回一個可以打開新 Activity 的 PendingIntent,相當于調用 ?Context.startActivity(Intent)。

static PendingIntent

  getBroadcast(Context context, int requestCode, Intent intent, int flags)

  返回一個可以發送廣播的 PendingIntent,相當于調用 Context.sendBroadcast()。

static PendingIntent

  getService(Context context, int requestCode, Intent intent, int flags)

  返回一個可以啟動服務的 PendingIntent,相當于調用 ?Context.startService()。

?

?

轉載于:https://www.cnblogs.com/sunzn/p/3503769.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377369.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377369.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377369.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

erp開發模式_ERP的完整形式是什么?

erp開發模式ERP:企業資源計劃 (ERP: Enterprise Resource Planning) ERP is an abbreviation of Enterprise Resource Planning. It is incorporated business management that is executed by a lot of numerous business houses to enhance their productivity an…

關于placement new 和 placement delete的重載,以及basic_string重載new()實例

關于placement new 在https://blog.csdn.net/qq_42604176/article/details/111997397中已經介紹了placement new的形式。 它的形式為new()/delete().我們將分配好內存的指針送入括號中,就完成了初步的調用了。 其實我們可以定義放任何的東西到()內部。只放一個指針…

cwc云萊特鏈_CWC的完整形式是什么?

cwc云萊特鏈CWC:工作條件的改變 (CWC: Change in Working Conditions) CWC is an abbreviation of "Change in Working Conditions". CWC是“工作條件更改”的縮寫 。 It is an expression, which is commonly used in the Gmail platform. In a particu…

在eclipse中創建web項目(非myeclipse)

在eclipse中創建web項目(非myeclipse) 在eclipse中如何創建dynamic web project項目 本文的演示是從本地文件創建dynamic web project,從svn檢出的同時創建dynamic web project于此類似。我們推薦使用解壓版的tomcat6.x版本,來作為服務器。可以到http://…

opengl glut 編譯

新建工程glut dll工程,本來想創建lib,工程的,但是想起來,gl是狀態機機制。dll方便資源共享等。 添加兩個include目錄 各種手機電腦平臺,網絡多媒體開發,mmsplayer,QQ514540005 然后將目錄下的lib/glut下面所有的.c文件…

劃分數據集代碼(按照4:1的比例)以及根據各自文件名寫入txt文件

會將圖片分到兩個文件夾中&#xff1a; #include <opencv2/opencv.hpp> #include "opencv2/features2d.hpp" #include <vector> #include <algorithm> #include <iostream> #include "windows.h" #include <stdio.h> #incl…

bpo是什么意思_BPO的完整形式是什么?

bpo是什么意思BPO&#xff1a;業務流程外包 (BPO: Business Process Outsourcing) BPO is an abbreviation of Business process outsourcing. It is a convention of a company to another company which is an external provider of services or business operations process…

在進行 ASP.NET 開發時,有時候需要對頁面輸出的最終 HTML 源代碼進行控制

在進行 ASP.NET 開發時&#xff0c;有時候需要對頁面輸出的最終 HTML 源代碼進行控制&#xff0c;是頁面的 render 方法中很容易實現這個功能。下面就是一個實現的方法&#xff0c;注釋都在代碼中。 [c-sharp] view plaincopy <% Page Language"C#" %> <% …

FireFox插件SQLite Manager的使用

最近幾天開始高IOS數據庫來著&#xff0c;一開始就CoreData的學習&#xff0c;結果高了一天沒有一點進展。 沒法&#xff0c;還是先老實代碼著吧&#xff0c;不過用的火狐插件可視化數據庫的操作也是不錯的似乎。 網上搜了搜用法&#xff0c;還真沒找到有什么的&#xff0c;最后…

簡單的數據增強代碼(C++與opencv)

包括了圖片批量平移、旋轉、以及像素變換 #include <opencv2/opencv.hpp> #include "opencv2/features2d.hpp" #include <vector> #include <algorithm> #include <iostream> #include "windows.h" #include <stdio.h> #in…

aes模式_AES的完整形式是什么?

aes模式AES&#xff1a;高級加密標準 (AES: Advanced Encryption Standard) AES is an abbreviation of Advanced Encryption Standard, also known by its original name Rijndael. It is an arrangement of standard for the encryption of electronic data set up by the U.…

IOS ----UIButton用法詳解

這段代碼動態的創建了一個UIButton,并且把相關常用的屬性都列舉了.希望對大家有用. //這里創建一個圓角矩形的按鈕UIButton *button1 [UIButton buttonWithType:UIButtonTypeRoundedRect];// 能夠定義的button類型有以下6種&#xff0c;// typedef enum {// UIButtonTypeCusto…

針對一個class寫出它的內存管理池以及總結出allocator類(三個版本)

目錄示例版本1&#xff1a;per-class allocator,1示例版本2&#xff1a;per-class allocator,2最終版本&#xff1a;static allocator針對版本三進行macro如果我們不針對對象做內存管理&#xff0c;那么我們每次進行Foo* p new Foo(x);時總是會調用malloc函數。 盡管malloc函數…

kotlin 第一個程序_Kotlin程序添加兩個矩陣

kotlin 第一個程序Given two matrices, we have to add them. 給定兩個矩陣&#xff0c;我們必須將它們相加。 Example: 例&#xff1a; Input:matrix 1:[2, 3][4, 5][7, 1]matrix 2:[4, 6][9, 0][7, 6]Output:[6, 9][13, 5][14, 7] 在Kotlin中添加兩個矩陣的程序 (Progra…

ubuntu 切換用戶的命令[shell, linux]

使用ubuntu過程中免不了和shell(終端)打交道, 也不可避免在各種用戶之間進行切換, 從而實現對各帳戶的管理, 這個就涉及到了一個比較基礎又很重要的工作,怎么樣切換用戶, 對于LINUX老鳥來說,這個根本不值不提的東東卻讓新手撓頭不已, 現在給出普通用戶和超級用戶切換的命令(附圖…

曲苑雜壇--修改數據庫名和文件組名

/* 該腳本示例如何完整的修改一個數據庫的名稱. 數據庫為原名稱為DB_BEIJING&#xff0c;需要修改成DB_SHANGHAI nzperfect 2012.12.19 */--判斷是否存在同名的數據庫&#xff0c;以防止誤刪除 USE master GO IF EXISTS (SELECT name FROM sys.databases WHERE name NDB_BEIJI…

關于new handler與default、delete關鍵字

在https://blog.csdn.net/qq_42604176/article/details/111638568的operate_new源代碼長啥樣中談到過new handler。 當operator new不能夠分配出申請的內存時&#xff0c;會拋出bad_alloc 異常。有的編譯器會返回0. 當定義成new(nothrow) Foo&#xff1b;就不會拋異常&#xff…

模式匹配運算符–Shell

轉載&#xff1a;http://www.firefoxbug.net/?p722 Var/home/firefox/MyProgram/fire.login.name ${Variable#pattern}:如果模式匹配于變量值的開頭處&#xff0c;則刪除匹配的最短部分&#xff0c;并且返回剩下的部分 例子&#xff1a; [fire]$ echo ${Var#*/} [fire]$ home/…

河內塔問題_河內塔的Python程序

河內塔問題You are challenged for a challenge to find the number of moves required to move a stack of disks from one peg to another peg. Wait for a second, it sounds easy? Let’s find are what is going on and in this article, we are introducing a chapter o…

VC6、BC5、G2.9標準分配器一覽

目錄VC6標準分配器BC5標準分配器G2.9標準分配器VC6標準分配器 VCx中源碼可以在電腦路徑中找&#xff1a; [D:\Program Files\VisualStudio\Community\VC\Tools\MSVC\14.28.29333\include\xmemory] 不過太多了。大概在837行左右有關于allocator代碼。還是先看侯捷PPT上的吧。 …