对应AlarmManage有一个AlarmManagerServie服务程 序,该服务程序才是正真提供闹铃服务的,它主要维护应用程序注册下来的各类闹铃并适时的设置即将触发的闹铃给闹铃设备(在系统中,linux实现的设备名 为”/dev/alarm”),并且一直监听闹铃设备,一旦有闹铃触发或者是闹铃事件发生,AlarmManagerServie服务程序就会遍历闹铃列 表找到相应的注册闹铃并发出广播。该服务程序在系统启动时被系统服务程序system_service启动并初始化闹铃设备(/dev/alarm)。当 然,在JAVA层的AlarmManagerService与Linux Alarm驱动程序接口之间还有一层封装,那就是JNI。

  AlarmManager将应用与服务分割开来后,使得应用程序开发者不用 关心具体的服务,而是直接通过AlarmManager来使用这种服务。这也许就是客户/服务模式的好处吧。AlarmManager与 AlarmManagerServie之间是通过Binder来通信的,他们之间是多对一的关系。

在android系统中,AlarmManage提供了3个接口5种类型的闹铃服务。

3个接口:

  1. // 取消已经注册的与参数匹配的闹铃
  2. void    cancel(PendingIntent operation)
  3. //注册一个新的闹铃
  4. void    set( int  type,  long  triggerAtTime, PendingIntent operation)
  5. //注册一个重复类型的闹铃
  6. void    setRepeating( int  type,  long  triggerAtTime,  long  interval, PendingIntent operation)
  7. //设置时区
  8. void    setTimeZone(String timeZone)
  1. // 取消已经注册的与参数匹配的闹铃
  2. void   cancel(PendingIntent operation)
  3. //注册一个新的闹铃
  4. void   set(int type, long triggerAtTime, PendingIntent operation)
  5. //注册一个重复类型的闹铃
  6. void   setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
  7. //设置时区
  8. void   setTimeZone(String timeZone)

5个闹铃类型

  1. public   static   final   int  ELAPSED_REALTIME
  2. // 当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时 间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3    (0x00000003)。
  3. public   static   final   int  ELAPSED_REALTIME_WAKEUP
  4. //能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。
  5. public   static   final   int  RTC
  6. //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用 System.currentTimeMillis()获得。系统值是1 (0x00000001) 。
  7. public   static   final   int  RTC_WAKEUP
  8. //能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。
  9. Public static   final   int  POWER_OFF_WAKEUP
  10. //能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同RTC类型,系统值为4(0x00000004)。
  1. public
  2. static final int ELAPSED_REALTIME
  3. //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠
  4. 时间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3    (0x00000003)。
  5. public static final int ELAPSED_REALTIME_WAKEUP
  6. //能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。
  7. public static final int RTC
  8. //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用
  9. System.currentTimeMillis()获得。系统值是1 (0x00000001) 。
  10. public static final int RTC_WAKEUP
  11. //能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。
  12. Public static final int POWER_OFF_WAKEUP
  13. //能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同RTC类型,系统值为
  14. 4(0x00000004)。

注意一个重要的参数PendingIntent。这个PendingIntent可以说是 Intent的进一步封装,他既包含了Intent的描述又是Intent行为的执行(这种定义也许不太严格),如果将Intent比作成一个订单的 话,PendingIntent更像是一个下订单的人,因为它既要负责将订单发出去,也要负责订单发送后的处理,比如发送成功后要准备验收订单货物,发送 失败后要重发还是取消订单等操作。开发者可以通过调用getActivity(Context, int, Intent, int)

getBroadcast(Context, int, Intent, int)

getService(Context, int, Intent, int)

三种不同方式来得到一个PendingIntent实例。

getBroadcast——通过该函数获得的PendingIntent将会 扮演一个广播的功能,就像调用 Context.sendBroadcast()函数一样。当系统通过它要发送一个intent时要采用广播的形式,并且在该intent中会包含相应的 intent接收对象,当然这个对象我们可以在创建PendingIntent的时候指定,也可以通过ACTION 和CATEGORY等描述让系统自动找到该行为处理对象。

  1. Intent intent =  new  Intent(AlarmController. this , OneShotAlarm. class );
  2. PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this ,  0 , intent,  0 );
  1. Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
  2. PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0);

getActivity——通过该函数获得的PendingIntent可以直 接启动新的activity, 就像调用 Context.startActivity(Intent)一样.不过值得注意的是要想这个新的Activity不再是当前进程存在的Activity 时。我们在intent中必须使用Intent.FLAG_ACTIVITY_NEW_TASK.

  1. // The PendingIntent to launch our activity if the user selects this notification
  2. PendingIntent contentIntent = PendingIntent.getActivity(this ,  0 ,   new  Intent( this , AlarmService. class ),  0 );
  1. // The PendingIntent to launch our activity if the user selects this notification
  2. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  new Intent(this, AlarmService.class), 0);

getService——通过该函数获得的PengdingIntent可以直接启动新的Service,就像调用Context.startService()一样。

  1. // Create an IntentSender that will launch our service, to be scheduled
  2. // with the alarm manager.
  3. mAlarmSender = PendingIntent.getService(AlarmService.this ,
  4. 0 ,  new  Intent(AlarmService. this , AlarmService_Service. class ),  0 );

深入学习android之AlarmManager的更多相关文章

  1. 一培训机构设计的学习android课程内容:供大家参考

    转自:http://www.cnblogs.com/csj007523/archive/2011/06/16/2082682.html 一培训机构设计的学习android课程内容:供大家参考 第一阶段 ...

  2. Android中AlarmManager使用示例(持续更新,已经更改)

    现在普遍的手机都会有一个闹钟的功能,如果使用Android来实现一个闹钟可以使用AtarmManager来实现.AtarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服 ...

  3. 想学习Android开发

    最近被别人说知识面窄,心里受伤了.准备学学Android开发,如果能在手机里运行自己写的app,那是多么high ~~~ Android开发需要看什么资料呢? 说明:本人一直从事windows下的C+ ...

  4. 从零开始学习Android(一)Android环境的搭建

    好久没有开始写博客了,最近开始学习Android,所以想把学习的笔记都一一记录下来.一来是方便自己以后资料的查询,其次也是给Android新手朋友进行学习使用,再次也希 望得到高手的指点.废话少说,我 ...

  5. 《IT蓝豹》吹雪花demo,学习android传感器

    吹雪花demo,学习android传感器 吹雪花demo,学习android传感器,嘴巴对着手机底部吹一下就会出现飘着雪花效果. 算是学习android传感器效果.本例子主要是通过android.me ...

  6. Android闹钟 AlarmManager的使用

    Android闹钟 AlarmManager的使用 AlarmManager介绍 AlarmManager这个类提供对系统闹钟服务的访问接口. 你可以为你的应用设定一个在未来某个时间唤醒的功能. 当闹 ...

  7. 一起来学习Android自定义控件1

    概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...

  8. Android 的 AlarmManager 和 wakeLock联合使用

    http://stackoverflow.com/questions/6864712/android-alarmmanager-not-waking-phone-up 主要说的是,对于android ...

  9. 10个很棒的学习Android 开发的网站(转)

    看到江湖旅人 写的<10个很棒的学习iOS开发的网站 - 简书>,所以就忍不住写Android 啦,也希望对大家有帮助.我推荐的网站,都是我在学习Android 开发过程中发现的好网站,给 ...

随机推荐

  1. linux作业六——进程的描述和进程的创建

    进程的描述和进程的创建 一.进程描述符task_struct 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. 代码关键点: 1.Struct list_hea ...

  2. Ubuntu 下为 Idea 创建启动图标.

    默认情况下,ubuntu将自动安装的软件快捷方式保存在/usr/share/applications目录下,如果我们要创建桌面快捷方式,需要在该目录下创建一个名为“Idea.desktop”的文件.通 ...

  3. C语言基础:两个变量交换值的方法

    学习任何语言基础时,两个数值得交换是必须掌握的,下面是3种不同的方式(c语言) 方法一:利用数学的计算技巧 #include <stdio.h> int main() { , b = ; ...

  4. 【转】android 选取图片

    转自:http://www.cnblogs.com/top5/archive/2012/03/06/2381986.html 这几天 在学习并开发android系统的图片浏览 音频 视频 的浏览 由于 ...

  5. ubuntu下修改ip重启系统ip不变

    今天同学问我ubuntu下ip如何写死,我想起这周在公司我们队长也问过我,我就在这把我实验的方法说一下. 打开终端: sudo vim /etc/network/interfaces 然后按如下修改: ...

  6. Pentaho Data Integration笔记 (一):安装

    介绍 Pentaho Data Integration (PDI) is an extract, transform, and load (ETL) solution that uses an inn ...

  7. c++ break while

    #include <iostream> #include <vector> #include <pthread.h> #include "destory_ ...

  8. css3技巧——产品列表之鼠标滑过效果(一)

    查看效果: http://www.daqianduan.com/example?pid=6117 html代码: <div class="main"> <div ...

  9. 【BZOJ】【2179】FFT快速傅里叶

    FFT 做的第二道用到FFT的……好吧其实还是模板题-_-b 百度上说好像分治也能做……不过像FFT这种敲模板的还是省事=.= /*********************************** ...

  10. springMVC+ibatis数据持久化入门级学习例子

    1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...