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.mzph.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上的吧。 …