AlarmManager、PaddingIntent的使用\
\附件Home监听十分钟后再次启动应用取消服务
对应AlarmManage有一个AlarmManagerServie服务程 序,该服务程序才是正真提供闹铃服务的,它主要维护应用程序注册下来的各类闹铃并适时的设置即将触发的闹铃给闹铃设备(在系统中,linux实现的设备名 为”/dev/alarm”),并且一直监听闹铃设备,一旦有闹铃触发或者是闹铃事件发生,AlarmManagerServie服务程序就会遍历闹铃列 表找到相应的注册闹铃并发出广播。该服务程序在系统启动时被系统服务程序system_service启动并初始化闹铃设备(/dev/alarm)。当 然,在JAVA层的AlarmManagerService与Linux Alarm驱动程序接口之间还有一层封装,那就是JNI。
AlarmManager将应用与服务分割开来后,使得应用程序开发者不用 关心具体的服务,而是直接通过AlarmManager来使用这种服务。这也许就是客户/服务模式的好处吧。AlarmManager与 AlarmManagerServie之间是通过Binder来通信的,他们之间是多对一的关系。
在android系统中,AlarmManage提供了5个接口5种类型的闹铃服务。
5个接口:
- // 取消已经注册的与参数匹配的闹铃
- void cancel(PendingIntent operation)
- //注册一个新的闹铃
- void set(int type, long triggerAtTime, PendingIntent operation)
- //注册一个重复类型的闹铃
- void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
- //设置时区 设置系统的默认时区。需要android.permission.SET_TIME_ZONE权限
- void setTimeZone(String timeZone)
- void setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
设置一个重复闹钟的不精确版本,它相对而言更节能(power-efficient)一些,因为系统可能会将几个差不多的闹钟合并为一个来执行,减少设备的唤醒次数。
内置的几个interval为:
INTERVAL_FIFTEEN_MINUTES
INTERVAL_HALF_HOUR
INTERVAL_HOUR
INTERVAL_HALF_DAY
INTERVAL_DAY
如果你将其设为DAY,那么可能这一天中的所有闹钟都会被合并掉。
5个闹铃类型
- public static final int ELAPSED_REALTIME
- // 当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时 间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3 (0x00000003)。
- 在指定的延时过后,发送广播,但不唤醒设备。
- public static final int ELAPSED_REALTIME_WAKEUP
- //能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。
- 在指定的演示后,发送广播,并唤醒设备
延时是要把系统启动的时间SystemClock.elapsedRealtime()算进去的 - public static final int RTC
- //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用 System.currentTimeMillis()获得。系统值是1 (0x00000001) 。
- 在指定的时刻,发送广播,但不唤醒设备
- public static final int RTC_WAKEUP
- //能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。
- 在指定的时刻,发送广播,并唤醒设备
- Public static final int POWER_OFF_WAKEUP
- //能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同RTC类型,系统值为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等描述让系统自动找到该行为处理对象。
- Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
- PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0);
getActivity——通过该函数获得的PendingIntent可以直 接启动新的activity, 就像调用 Context.startActivity(Intent)一样.不过值得注意的是要想这个新的Activity不再是当前进程存在的Activity 时。我们在intent中必须使用Intent.FLAG_ACTIVITY_NEW_TASK.
- // The PendingIntent to launch our activity if the user selects this notification
- PendingIntent contentIntent = PendingIntent.getActivity(this , 0 , new Intent( this , AlarmService. class ), 0 );
getService——通过该函数获得的PengdingIntent可以直接启动新的Service,就像调用Context.startService()一样。
- PendingIntent mAlarmSender = PendingIntent.getService(AlarmService.this ,
- 0 , new Intent(AlarmService. this , AlarmService_Service. class ), 0 );
5s后发送指定广播
- AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
- Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
- int requestCode = 0;
- PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(),
- requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- // 5秒后发送广播,只发送一次
- int triggerAtTime = SystemClock.elapsedRealtime() + 5 * 1000;
- alarmMgr.set(AlarmManager.ELAPSED_REALTIME, triggerAtTime, pendIntent);
5s后发送指定广播,然后每个10秒重复发送广播
- AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
- Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
- int requestCode = 0;
- PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(),
- requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- // 5秒后发送广播,然后每个10秒重复发广播。广播都是直接发到AlarmReceiver的
- int triggerAtTime = SystemClock.elapsedRealtime() + 5 * 1000;
- int interval = 10 * 1000;
- alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, interval, pendIntent);
取消一个闹钟
- AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
- Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
- PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(),
- 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- // 与上面的intent匹配(filterEquals(intent))的闹钟会被取消
- alarmMgr.cancel(pendIntent);
相关推荐
NULL 博文链接:https://technicalsearch.iteye.com/blog/2007636
根据本讲学习内容,学会运用AlarmManager和PendingIntent实现定时提醒功能。 (1) 提交的作业包中含有应用程序运行效果截图。
android安卓闹铃服务AlarmManager的使用
在Android平台,如何使用AlarmManager启动广播BroeadReceiver 、服务Service 、页面Activity。
Andorid AlarmManager的使用,通过这个实例可以变换实现闹钟功能
本篇通过AlarmManager和PendingIntent实现定时提醒功能,界面运用Button、TextView、Switch进行布局 同时这也是中国大学慕课移动终端应用开发的网课作业18,我会持续更新我的作业,如果有需要关注一下吧 说明 具体的...
实现可定时响起的闹钟----PendingIntent 与 AlarmManager 的运用
1. 通过 AlarmManager 来设置定制任务 2. 用广播去启动Service 3. Service中onStartCommand()方法执行任务
使用AlarmManager多次定时提醒 实现多次定时提醒的原理是: 在添加时间点后, 需要将所添加的时间点保存在文件或数据库中。
由于锁屏后CPU休眠,导致心跳包线程被挂起,所以尝试使用alarmManager定时唤醒Service发送心跳包。 以下是开启alarmManager的代码 //开启轮询服务 public static void startPollingService(Context context, int...
android AlarmManager 多个 定时 重复提醒,我这篇博文的源码。
AlarmManager提供了访问系统闹钟的服务。它允许你安排你的应用在未来某个时间点运行。这就相当于定时任务。对于少于60s的定时任务,不推荐使用AlarmManager,可以使用更高效的的Handler来处理频发的任务。
http://blog.csdn.net/two_water/article/details/52004414 博客Demo
AlarmManager实质是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service,BroadcastReceiver)。本文将讲解一下如何使用AlarmManager实现定时提醒...
Android中使用AlarmManager设置闹钟示例代码
AlarmManager 闹铃(支持重启) 设置多个闹铃 备注详细
Android AlarmManager
Android 后台运行 定时器 AlarmManager Service
本文实例讲述了Android闹铃服务AlarmManager用法。分享给大家供大家参考,具体如下: 对应AlarmManage有一个AlarmManagerServie服务程 序,该服务程序才是正真提供闹铃服务的,它主要维护应用程序注册下来的各类闹铃...
本地通知的基本目的是使应用程序能够在应用程序不在前台运行时通知其用户它有一些东西给他们——例如,一条消息或即将到来的约会。 它们由应用程序调度并在同一设备上交付。 它们如何呈现给用户 用户通过以下方式...