对应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. Java 8 VM GC Tuning Guide Charter3-4

    第三章 Generations One strength of the Java SE platform is that it shields the developer from the compl ...

  2. 《C++Primer》复习——with C++11 [2]

    1.数组引用形参,C++允许将变量定义成数组的引用,给予同样的道理,形参也可以是数组的引用,此时引用形参绑定到对应的实参上,也就是绑定到数组上 ]) { for (auto elem : arr) c ...

  3. org.apache.commons.dbutils.QueryRunner 执行sqlserver的存储过程

    执行不带输出参数的存储过程与 执行普通update  sql没有什么区别,直接调用即可: 示例代码: public Boolean startResidentialInfoStatistics(Str ...

  4. linux下php多版本的并存实现

    其实最简单的方法,就是通过nginx,生成多个php使用不同的端口,这实在简单,我写了两个版本,一个是apche服务,一个是nginx服务,使用一两个不同的版本,爽!

  5. 20160723数据结构节alexandrali

    大坑最后再填. 20160803:心情好回来填啦(5/7) 做的题目是: poj2970 我们先每个人都不给钱qwq 然后我们发现有一位的工作时间超过了d 那么我们就从以前安排过工作的人里,a最大的, ...

  6. Aizu 2325 Mysterious Maze

    走迷宫 ~ 不同的是题目给了你转向的方向序列 dis[x][y]表示到(x,y) 使用了最少的转向次数 #include<cstdio> #include<cstring> # ...

  7. 转载一个不错的Scrapy学习博客笔记

    背景: 最近在学习网络爬虫Scrapy,官网是 http://scrapy.org 官方描述:Scrapy is a fast high-level screen scraping and web c ...

  8. 关于nginx限制IP或IP段的问题2011

    关于nginx限制IP或IP段的问题2011-04-08 16:46:39 分类: LINUX 最近有同事问需要在nginx中针对一些IP和IP段限制访问,通过了解以下方法可以解决问题:   首先建立 ...

  9. IDataParameter[]

    IDataParameter[]简单的说来,就是可以包含任何数据库参数的数组,这些参数可以是SqlParameter或者OleDbParameter,但是,IDataParameter[]不可以直接从 ...

  10. Oracle的学习二:表管理(数据类型、创建/修改表、添加/修改/删除数据、数据查询)

    1.Oracle表的管理 表名和列名的命名规则: 必须以字母开头: 长度不能超过30个字符: 不能使用oracle的保留字: 只能使用如下字符:A-Z, a-z, 0-9, $, # 等. Oracl ...