对应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. Asp.net弹出层并且有遮罩层

    长久以来,asp.net弹出层并且有遮罩层问题都是一个难以解决的问题,鉴于此,我决定写个弹出层发布出来,供大家使用... 这里的doing层是遮罩层,divLogin层是登陆层 若有其他问题请留言或邮 ...

  2. Mysql几种索引类型的区别及适用情况

    如大家所知道的,Mysql目前主要有以下几种索引类型:FULLTEXT,HASH,BTREE,RTREE. 那么,这几种索引有什么功能和性能上的不同呢? FULLTEXT 即为全文索引,目前只有MyI ...

  3. 工程移除CocoaPods依赖库

    http://zanderzhang.gitcafe.io/2015/09/26/工程移除CocoaPods依赖库/ 点这里--->CocoaPods安装和使用教程 当我们工程安装很多第三方开源 ...

  4. js获取对象、数组的实际长度,元素实际个数

    /*获取对象.数组的长度.元素个数 *@param obj 要计算长度的元素,可以为object.array.string */ function count(obj){ var objType = ...

  5. Eclipse中创建标准web工程以及标准目录结构说明

    最近公司有个Web项目,项目结构如下: 虽然运行没有错,但是实在是别扭,标准的web应用一般不采用这种结构: 因此总结一下:     1.如何在Eclipse中创建一个标准的Web应用.     2. ...

  6. 【POJ】【2096】Collecting Bugs

    概率DP/数学期望 kuangbin总结中的第二题 大概题意:有n个子系统,s种bug,每次找出一个bug,这个bug属于第 i 个子系统的概率为1/n,是第 j 种bug的概率是1/s,问在每个子系 ...

  7. SQL Server 之 解锁

    下图,制作了一个可以维持1分钟的表锁: 下图,可以查询出被锁的表,其中 spid 是锁定表的进程ID(也是 session_id): 可以通过 select connect_time from sys ...

  8. 坑人的 try catch finally

    一直以为这样可以关闭 fs, 其实不行 static void Main(string[] args) { FileStream fs = null; try { fs = new FileStrea ...

  9. Chp17: Moderate

    17.1 swap a number in place.(without temporary variables) a = a ^ b; b = a ^ b; a = a ^ b; 17.3 Writ ...

  10. Oracle函数+for循环

    create or replace function FilterMinganci(str in varchar) return varchar2 is filterWorld varchar2(10 ...