对应 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()。

Android 编程下 AlarmManager的更多相关文章

  1. Android 编程下 App Install Location

    从 API 8 开始(参考官方文档:App Install Location | Android Developers),你可以将你的应用安装在外部储存中(例如,安装到设备的 SD 卡上).这是一个可 ...

  2. Android 编程下的自定义 xmlns

    什么是 xmlns xmlns是 XML Namespaces 的缩写,中文名称是 XML命名空间. xmlns 使用规则 xmlns:namespace-prefix="namespace ...

  3. Android 编程下 DP、SP 以及屏幕像素密度

    有时需为视图属性指定大小尺寸值(通常以像素为单位,但有时也用点.毫米或英寸).最常见的属性有: 文字大小(Text Size),指设备上显示的文字像素高度: 边距(Margin),指定视图组件间的距离 ...

  4. Android 编程下 Touch 事件的分发和消费机制

    Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent ev). ...

  5. Android 编程下的四大组件之服务(Service)

    服务(Service) 是一种在后台运行,没有界面的组件,由其他组件调用开始.Android 中的服务和 Windows 中的服务是类似的东西,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类 ...

  6. Android 编程下的 Secret Code

    我们很多人应该都做过这样的操作,打开拨号键盘输入 *#*#4636#*#* 等字符就会弹出一个界面显示手机相关的一些信息,这个功能在 Android 中被称为 Android Secret Code, ...

  7. Android 编程下图片的内存优化

    1. 对图片本身进行操作 尽量不要使用 setImageBitmap.setImageResource. BitmapFactory.decodeResource 来设置一张大图,因为这些方法在完成 ...

  8. Android 编程下的代码混淆

    什么是代码混淆 Java 是一种跨平台的.解释型语言,Java 源代码编译成中间”字节码”存储于 class 文件中.由于跨平台的需要,Java 字节码中包括了很多源代码信息,如变量名.方法名,并且通 ...

  9. Android 编程下去除 ListView 上下边界蓝色或黄色阴影

    默认的情况下,在 ListView 滑动到顶部或者是底部的时候,会有黄色或者蓝色的阴影出现.在不同的版本上解决的方法是不同的,在 2.3 版本之前可以在 ListView 的属性中通过设置 andro ...

随机推荐

  1. 没有找到AdbWinApi.dll

    今天调试adb命令时遇到一些问题 1. 没有找到AdbWinApi.dll 2. adb server is out of date.  killing... ADB server didn't AC ...

  2. linux下的文本编辑器VI的使用命令

    1. 移动光标 H #移到屏幕的左上角 M #移到屏幕的中间行开头 L #移到屏幕的最后一行 [ #移到文件开始位置 (双击) ] #移到文件结束位置(双击) :n #移到文件的第n行 Ctrl + ...

  3. 阿里云k8s服务springboot项目应用升级时出现502错误

    背景 随着小步快跑.快速迭代的开发模式被越来越多的互联网企业认同和采用,应用的变更.升级频率变得越来越频繁.为了应对不同的升级需求,保证升级过程平稳顺利地进行,诞生了一系列的部署发布模式. 停机发布 ...

  4. Swift3 获取系统音量和监听系统音量

    使用时: //定义滑动条用于显示音量 @IBOutlet weak var volumSlider: UISlider! //处理声音,获取当前音量,并添加监听 handleVolum() 方法内容: ...

  5. 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  6. 将windows目录共享到linux

    1.将windows目录共享 2.安装cifs 3.  mount -t cifs -o username=电脑登陆用户名,password=电脑登陆用户密码 //127.0.0.1/abc /var ...

  7. 【Android】Android 8种对话框(Dialog)

    1.写在前面 Android提供了丰富的Dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮).列表.单选.多选.等待.进度条.编辑.自定义等多种形式,将在第2部分介绍 ...

  8. NoSQL 非关系数据库

    NoSQL 数据库的学习 Redis的Windows版本安装 待整理 redis 安装 关于分布式的网站介绍 NOSQL 几个网页 认识MongoDB Mongodb实现副本集和Mongodb副本集的 ...

  9. Oracle 12C -- Invisible Columns

    在12C中,当一个列被定义为"不可见"的时候,没有直接访问该列的sql语句是无法看到"不可见列"的,显式引用"不可见列"的语句是可以访问和操 ...

  10. Failed to resolve: 之一

    摘要:编译不通过提示错误如下:gradle文件里边对应:解决方案:在gradle文件里边加上.+,解决后gradle文件如下图所示:然后编译就能通过. 解决方案: 在gradle文件里边加上.+,解决 ...