AlarmManager
转自:http://blog.csdn.net/wangxingwu_314/article/details/8060312
1、AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent。简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为我们广播一个我们设定的Intent,通常我们使用 PendingIntent,PendingIntent可以理解为Intent的封装包,简单的说就是在Intent上在加个指定的动作。在使用Intent的时候,我们还需要在执行startActivity、startService或sendBroadcast才能使Intent有用。而PendingIntent的话就是将这个动作包含在内了。
定义一个PendingIntent对象。
PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);
2、AlarmManager的常用方法有三个:
(1)set(int type,long startTime,PendingIntent pi);
该方法用于设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作。
(2)setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);
该方法用于设置重复闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟首次执行时间,第三个参数表示闹钟两次执行的间隔时间,第三个参数表示闹钟响应动作。
(3)setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);
该方法也用于设置重复闹钟,与第二个方法相似,不过其两个闹钟执行的间隔时间不是固定的而已。
3、三个方法各个参数详悉:
(1)int type: 闹钟的类型,常用的有5个值:AlarmManager.ELAPSED_REALTIME、 AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、 AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。
AlarmManager.ELAPSED_REALTIME表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始),状态值为3;
AlarmManager.ELAPSED_REALTIME_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间,状态值为2;
AlarmManager.RTC表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间,状态值为1;
AlarmManager.RTC_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,状态值为0;
AlarmManager.POWER_OFF_WAKEUP表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间,状态值为4;不过本状态好像受SDK版本影响,某些版本并不支持;
(2)long startTime: 闹钟的第一次执行时间,以毫秒为单位,可以自定义时间,不过一般使用当前时间。需要注意的是,本属性与第一个属性(type)密切相关,如果第一个参数对 应的闹钟使用的是相对时间(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那么本属性就得使用相对时间(相对于 系统启动时间来说),比如当前时间就表示为:SystemClock.elapsedRealtime();如果第一个参数对应的闹钟使用的是绝对时间 (RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本属性就得使用绝对时间,比如当前时间就表示 为:System.currentTimeMillis()。
(3)long intervalTime:对于后两个方法来说,存在本属性,表示两次闹钟执行的间隔时间,也是以毫秒为单位。
(4)PendingIntent pi: 绑定了闹钟的执行动作,比如发送一个广播、给出提示等等。PendingIntent是Intent的封装类。需要注意的是,如果是通过启动服务来实现闹钟提 示的话,PendingIntent对象的获取就应该采用Pending.getService(Context c,int i,Intent intent,int j)方法;如果是通过广播来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法;如果是采用Activity的方式来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。如果这三种方法错用了的话,虽然不会报错,但是看不到闹钟提示效果。
4.举例说明:定义一个闹钟,5秒钟重复响应。
(1)MainActivity,在onCreate中完成:
- //创建Intent对象,action为ELITOR_CLOCK,附加信息为字符串“你该打酱油了”
- Intent intent = new Intent("ELITOR_CLOCK");
- intent.putExtra("msg","你该打酱油了");
- //定义一个PendingIntent对象,PendingIntent.getBroadcast包含了sendBroadcast的动作。
- //也就是发送了action 为"ELITOR_CLOCK"的intent
- PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);
- //AlarmManager对象,注意这里并不是new一个对象,Alarmmanager为系统级服务
- AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
- //设置闹钟从当前时间开始,每隔5s执行一次PendingIntent对象pi,注意第一个参数与第二个参数的关系
- // 5秒后通过PendingIntent pi对象发送广播
- am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),5*1000,pi);
- //创建Intent对象,action为ELITOR_CLOCK,附加信息为字符串“你该打酱油了”
- Intent intent = new Intent("ELITOR_CLOCK");
- intent.putExtra("msg","你该打酱油了");
- //定义一个PendingIntent对象,PendingIntent.getBroadcast包含了sendBroadcast的动作。
- //也就是发送了action 为"ELITOR_CLOCK"的intent
- PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);
- //AlarmManager对象,注意这里并不是new一个对象,Alarmmanager为系统级服务
- AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
- //设置闹钟从当前时间开始,每隔5s执行一次PendingIntent对象pi,注意第一个参数与第二个参数的关系
- // 5秒后通过PendingIntent pi对象发送广播
- am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),5*1000,pi);
那么启动MainActivity之后,由于定义了AlarmManager am,并且调用了am.setRepeating(...)函数,则系统每隔5s将会通过pi启动intent发送广播,其action为ELITOR_CLOCK。所以我们需要在Manifest.xml中注册一个receiver,同时自己定义一个广播接收器类。
(2)定义一个广播接收器类MyReceiver,重写onReceive()函数。
- public class MyReceiver extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- // TODO Auto-generated method stub
- Log.d("MyTag", "onclock......................");
- String msg = intent.getStringExtra("msg");
- Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
- }
- }
- public class MyReceiver extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- // TODO Auto-generated method stub
- Log.d("MyTag", "onclock......................");
- String msg = intent.getStringExtra("msg");
- Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
- }
- }
(3)在Manifest.xml中注册广播接收器:
- <receiver android:name=".MyReceiver">
- <intent-filter>
- <action android:name="ELITOR_CLOCK" />
- </intent-filter>
- </receiver>
- <receiver android:name=".MyReceiver">
- <intent-filter>
- <action android:name="ELITOR_CLOCK" />
- </intent-filter>
- </receiver>


补充:设置指定的时间启动广播。
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar c=Calendar.getInstance();
c.set(Calendar.YEAR,2011);
c.set(Calendar.MONTH,Calendar.JUNE);//也可以填数字,0-11,一月为0
c.set(Calendar.DAY_OF_MONTH, 28);
c.set(Calendar.HOUR_OF_DAY, 19);
c.set(Calendar.MINUTE, 50);
c.set(Calendar.SECOND, 0);
//设定时间为 2011年6月28日19点50分0秒
//c.set(2011, 05,28, 19,50, 0);
//也可以写在一行里
Intent intent=new Intent(this,AlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(this, 0, intent,0);
//设置一个PendingIntent对象,发送广播
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
//获取AlarmManager对象
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
//时间到时,执行PendingIntent,只执行一次
//AlarmManager.RTC_WAKEUP休眠时会运行,如果是AlarmManager.RTC,在休眠时不会运行
//am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 10000, pi);
//如果需要重复执行,使用上面一行的setRepeating方法,倒数第二参数为间隔时间,单位为毫秒
}
}
AlarmManager的更多相关文章
- Android中AlarmManager使用示例(持续更新,已经更改)
现在普遍的手机都会有一个闹钟的功能,如果使用Android来实现一个闹钟可以使用AtarmManager来实现.AtarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服 ...
- Android随笔之——闹钟制作铺垫之AlarmManager详解
说实话,之前写的两篇博客Android广播机制Broadcast详解.Android时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...
- 用alarmmanager 多次发送PendingIntent
遇到如下问题 service中得一随机数 用alarmmanager 发送PendingIntent的时候,receiver收到的随机数不变. pendingintent传值经常获取到的值是第一次的值 ...
- 转:在Android中使用AlarmManager
原地址http://blog.csdn.net/maosidiaoxian/article/details/21776697 AlarmManager是Android中的一种系统级别的提醒服务,它会为 ...
- Android中使用AlarmManager进程被删除的解决办法
http://blog.csdn.net/zhouzhiwengang/article/details/13022325 在Android中,AlarmManager提供了不受休眠状态的系统定时功能, ...
- Android闹钟 AlarmManager的使用
Android闹钟 AlarmManager的使用 AlarmManager介绍 AlarmManager这个类提供对系统闹钟服务的访问接口. 你可以为你的应用设定一个在未来某个时间唤醒的功能. 当闹 ...
- WakeLock, AlarmManager, JobScheduler
应用程序耗电的实质,是所启用的硬件在消耗电量. 手机的耗电单元 CPU: 应用处理器(AP)和基带处理器(BB或BP) GPU(图形处理单元) 外设:wifi,BT, GPS,LCD等 AP是ARM架 ...
- Android 的 AlarmManager 和 wakeLock联合使用
http://stackoverflow.com/questions/6864712/android-alarmmanager-not-waking-phone-up 主要说的是,对于android ...
- [Android] 查看Android中的AlarmManager事件
reference to : https://segmentfault.com/a/1190000000404684 有时候我们需要设置一个alarmmanager事件 但是如果这个事件的时间是凌晨三 ...
- AlarmManager 实现闹钟的基本功能
先上效果图 这是一个利用AlarmManager做的最简单的闹钟!迟点再把重复响铃(例如星期一,星期三,重复响铃) 1.MainActivity package com.example.domeref ...
随机推荐
- Machine Learning - 第6周(Advice for Applying Machine Learning、Machine Learning System Design)
In Week 6, you will be learning about systematically improving your learning algorithm. The videos f ...
- 屏幕输出VS文件输出
问题1:我们在编写程序时经常需要数一些数据到屏幕,来查看我们的结果是否正确,虽然直接输出到屏幕,查看起来呢很方便,但当数据量很大时,需要耗费大量的时间.于是我们想到能不能通过输出到文件来减少时间 ...
- linux笔记:linux常用命令-文件搜索命令
文件搜索命令:find(文件搜索) 一些示例: 注意:在以文件名为条件进行搜索时,支持通配符. 多条件搜索,以及直接对搜索到的文件进行操作: 文件搜索命令:locate(在文件资料库中查找文件) 文件 ...
- $.get的重写
window.meng = window.meng || {}; (function () { function Get() { this.def = $.Deferred(); } Get.prot ...
- 微信JS SDK Demo 官方案例
微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微信分享 ...
- js正则--验证6-12位至少包含数字、小写字母和大些字母中至少两种字符,
var reg=/^((([a-z])+([0-9])+)|(([0-9])+([a-z])+)|(([A-Z])+([0-9])+)|(([0-9])+([A-Z])+)|(([a-z])+([A- ...
- jq--回到顶部
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- cf--------(div1)1A. Theatre Square
A. Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...
- 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- SQL Server 一列或多列重复数据的查询,删除
业务需求 最近给公司做一个小工具,把某个数据库(数据源)的数据导进另一个数据(目标数据库).要求导入目标数据库的数据不能出现重复.但情况是数据源本身就有重复的数据.所以要先清除数据源数据. 于是就把关 ...