Android闹钟 AlarmManager的使用
Android闹钟 AlarmManager的使用
AlarmManager介绍
AlarmManager这个类提供对系统闹钟服务的访问接口。
你可以为你的应用设定一个在未来某个时间唤醒的功能。
当闹钟响起,实际上是系统发出了为这个闹钟注册的广播,会自动开启目标应用。
注册的闹钟在设备睡眠的时候仍然会保留,可以选择性地设置是否唤醒设备,但是当设备关机和重启后,闹钟将会被清除。
在alarm的receiver的onReceive()方法被执行的时候,Alarm Manager持有一个CPU唤醒锁,这样就保证了设备在处理完广播之前不会sleep。
一旦onReceive()方法返回,Alarm Manager就会释放这个锁,表明一些情况下可能onReceive()方法一执行完设备就会sleep。
如果你的alarm receiver中调用了Context.startService(),那么很可能service还没起来设备就sleep了。
为了阻止这种情况,你的BroadcastReceiver和Service需要实现不同的唤醒锁机制,来确保设备持续运行到service可用为止。
注意:Alarm Manager主要是用来在特定时刻运行你的代码,即便是你的应用在那个特定时刻没有跑的情况。
对于常规的计时操作(ticks, timeouts, etc),使用Handler处理更加方便和有效率。
另:从API 19开始,alarm的机制都是非准确传递,操作系统将会转换闹钟,来最小化唤醒和电池使用。
有一些新的API会支持严格准确的传递,见 setWindow(int, long, long, PendingIntent)和setExact(int, long, PendingIntent)。
targetSdkVersion在API 19之前应用仍将继续使用以前的行为,所有的闹钟在要求准确传递的情况下都会准确传递。
闹钟Demo
Android Api demos中就有关于闹钟使用的Demo:
com.example.android.apis.app.AlarmController
其中设定了两种闹钟,一种是一次性的,一种是重复的。
Manifest中的声明,process属性
自定义的receiver,在manifest中声明如下:
<receiver
android:name=".OneShotAlarm"
android:process=":remote" />
<receiver
android:name=".RepeatingAlarm"
android:process=":remote" />
Demo中两个Receiver的onReceive方法中显示了各自的Toast提示,所以不再列出。
在此讨论一下process属性,它规定了组件(activity, service, receiver等)所在的进程。
通常情况下,没有指定这个属性,一个应用所有的组件都运行在应用的默认进程中,进程的名字和应用的包名一致。
比如manifest的package="com.example.helloalarm",则默认进程名就是com.example.helloalarm。
<application>元素的process属性可以为全部的组件设置一个不同的默认进程。
组件可以override这个默认的进程设置,这样你的应用就可以是多进程的。
如果你的process属性以一个冒号开头,进程名会在原来的进程名之后附加冒号之后的字符串作为新的进程名。当组件需要时,会自动创建这个进程。这个进程是应用私有的进程。
如果process属性以小写字母开头,将会直接以属性中的这个名字作为进程名,这是一个全局进程,这样的进程可以被多个不同应用中的组件共享。
一次性闹钟
// When the alarm goes off, we want to broadcast an Intent to our
// BroadcastReceiver. Here we make an Intent with an explicit class
// name to have our own receiver (which has been published in
// AndroidManifest.xml) instantiated and called, and then create an
// IntentSender to have the intent executed as a broadcast.
Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(
AlarmController.this, 0, intent, 0); // We want the alarm to go off 10 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10); // Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
重复闹钟
闹钟设置:
// When the alarm goes off, we want to broadcast an Intent to our
// BroadcastReceiver. Here we make an Intent with an explicit class
// name to have our own receiver (which has been published in
// AndroidManifest.xml) instantiated and called, and then create an
// IntentSender to have the intent executed as a broadcast.
// Note that unlike above, this IntentSender is configured to
// allow itself to be sent multiple times.
Intent intent = new Intent(AlarmController.this,
RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(
AlarmController.this, 0, intent, 0); // We want the alarm to go off 10 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 10 * 1000, sender);
闹钟取消:
// Create the same intent, and thus a matching IntentSender, for
// the one that was scheduled.
Intent intent = new Intent(AlarmController.this,
RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(
AlarmController.this, 0, intent, 0); // And cancel the alarm.
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(sender);
AlarmManager说明
AlarmManager这个类提供对系统闹钟服务的访问接口。
对它的获取是通过系统服务:
Context.getSystemService(Context.ALARM_SERVICE)。
相关方法说明:
cancel(PendingIntent operation)方法将会取消Intent匹配的任何闹钟。
关于Intent的匹配,查看filterEquals(Intent other)方法的说明可知,两个Intent从intent resolution(filtering)(Intent决议或过滤)的角度来看是一致的,即认为两个Intent相等。即是说,Intent的action,data,type,class,categories是相同的,其他的数据都不在比较范围之内。
set(int type, long triggerAtMillis, PendingIntent operation)方法将会设置一个闹钟。
注意:对于计时操作,可能使用Handler更加有效率和简单。
设置闹钟的时候注意:
1.如果声明的triggerAtMillis是一个过去的时间,闹钟将会立即被触发。
2.如果已经有一个相同intent的闹钟被设置过了,那么前一个闹钟将会取消,被新设置的闹钟所代替。
注意这里说的intent相同指的都是Intent在 filterEquals(Intent)的定义下匹配。
闹钟是一个广播,接收器需要自己定义和注册,注册使用动态注册( registerReceiver(BroadcastReceiver, IntentFilter) )或者静态注册(<receiver> tag in an AndroidManifest.xml file)都可以。
setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)方法将会设置一个重复性的闹钟。
比set方法多了一个间隔参数。
type的类型是四种:
ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC, RTC_WAKEUP.
区分的是时间标准和是否在睡眠状态下唤醒设备。
具体查看官方文档吧不再详细解释啦。
实例
比如要设置一个每晚21:30唤醒的重复闹钟:
private static final int INTERVAL = 1000 * 60 * 60 * 24;// 24h
//...
Intent intent = new Intent(context, RequestAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context,
REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// Schedule the alarm!
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 10);
calendar.set(Calendar.MILLISECOND, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
INTERVAL, sender);
参考资料
AlarmManager:
http://developer.android.com/reference/android/app/AlarmManager.html
闹钟Demo解析:
http://blog.csdn.net/mapdigit/article/details/7644134
Android闹钟 AlarmManager的使用的更多相关文章
- Android中AlarmManager使用示例(持续更新,已经更改)
现在普遍的手机都会有一个闹钟的功能,如果使用Android来实现一个闹钟可以使用AtarmManager来实现.AtarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服 ...
- Android闹钟设置的解决方案
Android设置闹钟并不像IOS那样这么简单,做过Android设置闹钟的开发者都知道里面的坑有多深.下面记录一下,我解决Android闹钟设置的解决方案. 主要问题 API19开始AlarmMan ...
- Android 闹钟设置
在Android中可以通过AlarmManager 来实现闹钟,AlarmManager类是专门用来设定在某个指定的时间去完成指定的事件.AlarmManager 提供了访问系统警报的服务,只要在程序 ...
- Android闹钟【复杂版】
最近做闹钟,所以自己写了个Demo版本,这个程序是用listview单独的类来实现的,和activity类分开来实现的!这个是用数据库进行更新的,当闹钟设置后,闹钟图片变成闹钟的样子,闹钟取消后,图片 ...
- 【源码】实现Android闹钟功能使用HTML+JS,并附带Alarm代码分享
先是开发手册中最终功能的实现函数,再是Alarm接口的源码. 参数: argu:型如“key1=value1;key2=value2;......”的参数表.首先,该参数表支持 rexseeNotif ...
- Android 开发 AlarmManager 定时器
介绍 AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为 ...
- 我的Android进阶之旅------>Android使用AlarmManager全局定时器实现定时更换壁纸
该DEMO将会通过AlarmManager来周期的调用ChangeService,从而让系统实现定时更换壁纸的功能. 更换壁纸的API为android.app.WallpaperManager,它提供 ...
- android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动
android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动 1.先简单设置一个闹钟提醒事件: //设置闹钟 mSetting.setOnClickListener ...
- Android利用AlarmManager执行定时任务
Android中的AlarmManager功能很强大,它是一个全局定时器,可以在指定时间或者指定周期启动其他组件(包括Activity.Service.BroadcastReceiver). 使用Al ...
随机推荐
- oracle创建表相关
--创建表 create table person( id number primary key, name ), birth date ); --创建序列 create sequence perso ...
- nodejs学习笔记三——nodejs使用富文本插件ueditor
在做自己的nodejs项目的时候遇到需要使用ueditor.原来下载的是ueditor的jsp版本.目录如下 在ueditor.config.js中有配置服务器home路径(这个home路径能找到u ...
- SQL Server安全(1/11):SQL Server安全概述
在保密你的服务器和数据,防备当前复杂的攻击,SQL Server有你需要的一切.但在你能有效使用这些安全功能前,你需要理解你面对的威胁和一些基本的安全概念.这篇文章提供了基础,因此你可以对SQL Se ...
- 基于HT for Web 快速搭建3D机房设备面板
以真实设备为模型,搭建出设备面板,并实时获取设备运行参数,显示在设备面板上,这相比于纯数值的设备监控系统显得更加生动直观.今天我们就在HT for Web的3D技术上完成设备面板的搭建. 我们今天模拟 ...
- Moon.Orm3.8技术全攻略
Moon.ORM技术全攻略 一.绪论 本文主要是针对Moon.ORM的技术的讨论及其使用使用指导.如有其它疑问,请留言.本文主要针对Moon.ORM3.9版本,同时将会对4.0做一个技术预览.本文从 ...
- 四、BLE(中)
1.1 GATT Manager GATT MGR模块管理所有的GATT服务,同时也是连接GATT模块与GATT ServiceS模块的桥梁. 1.1.1 主要功能模块 先来看一张该 ...
- [译]针对科学数据处理的统计学习教程(scikit-learn教程2)
翻译:Tacey Wong 统计学习: 随着科学实验数据的迅速增长,机器学习成了一种越来越重要的技术.问题从构建一个预测函数将不同的观察数据联系起来,到将观测数据分类,或者从未标记数据中学习到一些结构 ...
- Auto Mapper02《demo》
学习这些基本上网上都有一些教程或者别人做的demo,我是按照这个方式去学习的.先做个demo,学会如何去使用它,接着去慢慢的了解它是如何的运行的,理解里面的一些基本的基础知识.我们不可以再像 ...
- EF 中事务的书写
在EF 中怎么使用事务? 这个问题纠结了我好久,直到有人跟我一起讨论,我和同事一起讨论查资料. 查的好多资料都是使用 using (TransactionScope scope = new Trans ...
- sql server删除默认值(default)的方法
不废话了----- 例如要删除student表的sex默认值 sp_help student;查询结果 找到constraiont_name的对应的值 最后 ALTER TABLE student D ...