Android平台中,Alarm Manager Service控制着闹钟和唤醒功能。和其他系统服务一样,提供了一个辅助管理类-AlarmManager,我们只需要使用AlarmManager即可调用Alarm Manager Service。

  在AlarmManager提供了如下方法:

1、void cancel(pendingIntent operatioin):取消一个已注册的定时器

2、void set(int type,long triggerAtTime,PendingIntent operation):设置一个新的定时器。

3、void setInecactRepeating(int type,long triggerAtMillis,long intervalMills,PendingIntent operation):设置一个不精确的重复类型的定时器。

4、void setRepeating(int type,long triggerAtMills,long intervalMills,PendingIntent operation):设置一个重复类型的定时器

5、void setTime(long millis):设置系统时钟时间。

6、void setTimeZone(String timeZone):设置时区

在上面设置时钟的方法中,第一个参数要设置一个闹钟的类型,在系统中提供了如下类型:

1、ELAPSED_REALTIME:此类型的闹钟在系统休眠状态下是不可用的,不能唤醒系统。使用的时间是相对于系统的启动时间,该时间可以通过SystemClock.elapsedRealTime()来获取。

2、ELAPSED_REALTIME_WAKEUP:此类型的闹钟在系统休眠状态下是可用的,使用的时间是相对于系统的启动时间。

3、RTC:此类型在系统休眠状态下是不可用的,使用的是真实时间。

4、RTC_WAKEUP:此类型在系统休眠状态下可用,使用的是真实时间。

在上面的设置可重复的时钟的方法中,intervalMillis参数的取值可以如下:

1、INTERVAL_FIFTEEN_MINUTES

2、INTERVAL_HALF_HOUR

3、INTERVAL_HOUR

4、INTERVAL_HALF_DAY

5、INTERVAL_DAY

具体如何使用AlarmManager呢,我们通过一个案例来说明。

public class AlarmManagerActivity extends ActionBarActivity {

    private AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_manager);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
IntentFilter filter = new IntentFilter("com.jredu.action.MyAlarm");
registerReceiver(receiver,filter);
} public void setClock(View v){
Intent i = new Intent("com.jredu.action.MyAlarm");
PendingIntent intent=
PendingIntent.getBroadcast(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(),
30*1000,intent);
}
public void cancleClock(View v){
Intent i = new Intent("com.jredu.action.MyAlarm");
PendingIntent intent=
PendingIntent.getBroadcast(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(intent);
} private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("com.jredu.action.MyAlarm")){
Toast.makeText(AlarmManagerActivity.this,"这是我设置的闹钟!",Toast.LENGTH_LONG).show();
}
}
}; @Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}

运行效果图如下:

作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

技术咨询:
 

Android之AlarmManager的更多相关文章

  1. Android中AlarmManager使用示例(持续更新,已经更改)

    现在普遍的手机都会有一个闹钟的功能,如果使用Android来实现一个闹钟可以使用AtarmManager来实现.AtarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服 ...

  2. Android闹钟 AlarmManager的使用

    Android闹钟 AlarmManager的使用 AlarmManager介绍 AlarmManager这个类提供对系统闹钟服务的访问接口. 你可以为你的应用设定一个在未来某个时间唤醒的功能. 当闹 ...

  3. Android 的 AlarmManager 和 wakeLock联合使用

    http://stackoverflow.com/questions/6864712/android-alarmmanager-not-waking-phone-up 主要说的是,对于android ...

  4. Android 开发 AlarmManager 定时器

    介绍 AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为 ...

  5. 我的Android进阶之旅------>Android使用AlarmManager全局定时器实现定时更换壁纸

    该DEMO将会通过AlarmManager来周期的调用ChangeService,从而让系统实现定时更换壁纸的功能. 更换壁纸的API为android.app.WallpaperManager,它提供 ...

  6. Android利用AlarmManager执行定时任务

    Android中的AlarmManager功能很强大,它是一个全局定时器,可以在指定时间或者指定周期启动其他组件(包括Activity.Service.BroadcastReceiver). 使用Al ...

  7. android 定时器AlarmManager

    1.android中通常是使用AlarmManager来定时启动一个单次或重复多次操作的.具体的说就是我们通过AlarmManager设定一个时间和注册一个intent到系统中,然后在该时间到来时,系 ...

  8. android 通过AlarmManager实现守护进程

    场景:在app崩溃或手动退出或静默安装后能够自动重启应用activity 前提:得到系统签名 platform.pk8.platform.x509.pem及signapk.jar 三个文件缺一不可(系 ...

  9. 深入学习android之AlarmManager

    对应AlarmManage有一个AlarmManagerServie服务程 序,该服务程序才是正真提供闹铃服务的,它主要维护应用程序注册下来的各类闹铃并适时的设置即将触发的闹铃给闹铃设备(在系统中,l ...

  10. android之AlarmManager 全局定时器

    AlarmManager实质是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service,BroadcastReceiver ...

随机推荐

  1. [leetcode shell]192. Word Frequency

    统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{prin ...

  2. Python发送带附件的邮件

    看别人的博客就不要在往别人的邮箱里发东西了行不行, 有点素质可以吗!!! 写出来分享还不知道珍惜!!!!! #-*-encoding:utf-8 -*- import os import smtpli ...

  3. [SPOJ-BEADS]Glass Beads

    来源: CE1998 题目大意: 求字符串最小表示. 思路: 字符串复制一遍接在后面,构建SAM,然后每次跑小的转移. 跑n次以后就跑到了最小表示的末尾,用该状态的len值减去n就是最小表示的起始位置 ...

  4. hdu 4557 暴力

    题意: 作为2013年699万应届毕业生中的一员,由于宏观经济的不景气,小明在毕业当天就华丽丽地失业了! 经历了千难万苦的求职过程,小明特别能理解毕业生的就业之难,所以,他现在准备创建一家专门针对IT ...

  5. LeetCode:整数反转(Reserve Integer)

    public class ReserveInteger { public int reverse(int x) { //用于接收个位数(10的余数) int remainder; //是否负数 int ...

  6. 【洛谷】4917:天守阁的地板【欧拉函数的应用】【lcm与gcd】【同除根号优化】

    P4917 天守阁的地板 题目背景 在下克上异变中,博丽灵梦为了找到异变的源头,一路打到了天守阁 异变主谋鬼人正邪为了迎击,将天守阁反复颠倒过来,而年久失修的天守阁也因此掉下了很多块地板 异变结束后, ...

  7. js:深入prototype(上:内存分析)

    /**  * 下面演示了通过原型的创建方式,使用基于原型的创建能够将属性和方法  * 设置为Person专有的,不能通过window来调用.  * 原型是javascript中的一个特殊对象,当一个函 ...

  8. Android设备运用Clockworkmod Recovery恢复模式安装定制的Rom

    Clockworkmod Recovery是一个由Cyanogen团队开发的用于Android设备的第三方定制Recovery恢复模式,也称为CWM Recovery,具体它有什么用处呢?请看关于Go ...

  9. Redhat Enterprise Linux 7.4/CentOS 7.4 安装后初始化配置

    由于我是最小化安装,需要在安装后进行一些配置 1. 设定启动级别 [root@home ~]# systemctl set-default multi-user.target 2. 设定网络 [roo ...

  10. PostgreSQL 资源

    http://blog.163.com/digoal@126/blog/static/163877040201172183022203/ http://m.oschina.net/u/2426299? ...