闹钟的原理可用下面我自己画的一幅图来概括:(不对的地方,尽管吐槽

我们来看看新建闹钟到闹钟响铃的步骤:
 
 1、新建一个闹钟:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 获得AlarmManager实例
       final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
       
       // 实例化Intent
       Intent intent = new Intent();
       // 设置Intent action属性
       intent.setAction("com.test.BC_ACTION");
       intent.putExtra("msg", "该去开会啦!");
       // 实例化PendingIntent
       final PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,
               intent, 0);
       // 获得系统时间
       final long time = System.currentTimeMillis();
 am.set(AlarmManager.RTC_WAKEUP, time+5000, sender);//5秒后闹铃
 
       // 设置按钮单击事件
       setBtn.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               // 重复提示,从当前时间开始,间隔5秒
               am.setRepeating(AlarmManager.RTC_WAKEUP, time,
                       5 * 1000, pi);
           }
       });

在AndroidMainfest.xml里注册广播接收器

?
1
2
3
4
5
<receiverandroid:name="MyReceiver">
            <intent-filter>
                <actionandroid:name="com.test.BC_ACTION"/>
            </intent-filter>
        </receiver>

2、定义一个AlarmReceiver extends BroadcastReceiver接收广播,并弹出闹钟提醒视图。 


  


 上面用到一个AlarmManage,我们分别来看看它的处理闹钟流程和作用及例子。 


 处理闹钟流程:对应AlarmManage有一个AlarmManagerServie服务程序,该服务程序才是正真提供闹铃服务的,它主要遍历闹铃列表并设置即将触发的闹铃给闹铃设备,并且一直监听闹铃设备,一旦有闹铃触发或者是闹铃事件发生,AlarmManagerServie服务程序就会遍历闹铃列表找到相应的注册闹铃并发出广播。 


  


 作用及例子:AlarmManage中文名闹钟,或者叫做“全局定时器”更合适,它的作用和Timer类似,有两种使用方法:1、在特定时长后(特定时间)执行某任务;2、周期性的执行某任务,AlarmManager对象配合Intent使用,可以定时的开启一个Activity,发送一个BroadCast,或者开启一个Service. 


  


 (1)在指定时长后(特定时间)执行某项操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//操作:发送一个广播,广播接收后Toast提示定时操作完成
     Intent intent =newIntent(Main.this, alarmreceiver.class);
    intent.setAction("short");
    PendingIntent sender=
        PendingIntent.getBroadcast(Main.this,0, intent,0);
    
    //设定一个五秒后的时间
    Calendar calendar=Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND,5);
    
    AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    //或者以下面方式简化
    //alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender);
    
    Toast.makeText(Main.this,"五秒后alarm开启", Toast.LENGTH_LONG).show();

(2)周期性的执行某项操作

?
1
2
3
4
5
6
7
8
9
10
11
12
Intent intent =new Intent(Main.this, alarmreceiver.class);
    intent.setAction("repeating");
    PendingIntent sender=PendingIntent
        .getBroadcast(Main.this, 0, intent, 0);
    
    //开始时间
    long firstime=SystemClock.elapsedRealtime();
 
    AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
  //5秒一个周期,不停的发送广播
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP
            , firstime, 5*1000, sender);

AlarmManager的取消:(其中需要注意的是取消的Intent必须与启动Intent保持绝对一致才能支持取消AlarmManager)

?
1
2
3
4
5
6
Intent intent =newIntent(Main.this, alarmreceiver.class);
intent.setAction("repeating");
PendingIntent sender=PendingIntent
       .getBroadcast(Main.this,0, intent,0);
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
alarm.cancel(sender);

AlarmManager还将闹钟分为五种类型:

?
1
public  staticfinalintELAPSED_REALTIME          

//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠   


   时间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3 (0x00000003)。

?
1
publicstaticfinalintELAPSED_REALTIME_WAKEUP

//能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。

?
1
public static final int RTC

//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用   


   System.currentTimeMillis()获得。系统值是1 (0x00000001) 。

?
1
publicstaticfinalintRTC_WAKEUP

//能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。

?
1
PublicstaticfinalintPOWER_OFF_WAKEUP

//能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同RTC类型,系统值为4 (0x00000004)。

综上所述,感觉AlarmManage和NotificationManager差不多,NotificationManager例子请见文章http://my.oschina.net/helu/blog/141728

android闹钟实现原理的更多相关文章

  1. Android闹钟 AlarmManager的使用

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

  2. Android闹钟设置的解决方案

    Android设置闹钟并不像IOS那样这么简单,做过Android设置闹钟的开发者都知道里面的坑有多深.下面记录一下,我解决Android闹钟设置的解决方案. 主要问题 API19开始AlarmMan ...

  3. Android Touch事件原理加实例分析

    Android中有各种各样的事件,以响应用户的操作.这些事件可以分为按键事件和触屏事件.而Touch事件是触屏事件的基础事件,在进行Android开发时经常会用到,所以非常有必要深入理解它的原理机制. ...

  4. android handler工作原理

    android handler工作原理 作用 便于在子线程中更新主UI线程中的控件 这里涉及到了UI主线程和子线程 UI主线程 它很特别.通常我们会认为UI主线程将页面绘制完成,就结束了.但是它没有. ...

  5. android MultiDex multidex原理原理下遇见的N个深坑(二)

    android MultiDex 原理下遇见的N个深坑(二) 这是在一个论坛看到的问题,其实你不知道MultiDex到底有多坑. 不了解的可以先看上篇文章:android MultiDex multi ...

  6. android MultiDex multiDex原理(一)

    android MultiDex 原理(一) Android分包MultiDex原理详解 转载请注明:http://blog.csdn.net/djy1992/article/details/5116 ...

  7. 解析 Android Things 技术原理

    2012 年 6 月,由 IoT-GSI(Global Standards Initiative on Internet of Things)发布的白皮书“ITU-T Y.4000/Y.2060”[1 ...

  8. android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动

    android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动            1.先简单设置一个闹钟提醒事件: //设置闹钟 mSetting.setOnClickListener ...

  9. Android闹钟开发与展示Demo

    前言: 看过了不少安卓闹钟开发的例子,都是点到为止,都不完整,这次整一个看看. 一.闹钟的设置不需要数据库,但是展示闹钟列表的时候需要,所以需要数据库: public class MySQLiteOp ...

随机推荐

  1. html页面中块级元素的居中

    第一:在页面中使用 margin: 0 auto;居中: <div> <p>夜屋</p> </div> div { width: 100%; posit ...

  2. mac: brew的删除

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)" ...

  3. 2.FastJson公司--阿里巴巴开源的速度最快的Json和对象转换工具

    转自:https://blog.csdn.net/gongpulin/article/details/52062532 这是关于FastJson的一个使用Demo,在Java环境下验证的 这是关于Fa ...

  4. C#发送邮件DEMO

    虽然网上有很多类似的DEMO,但是还是整个封装好的例子,以便以后用: 发送邮箱是直接在web.config配置的. protected void Button1_Click(object sender ...

  5. Android NineGridLayout — 仿微信朋友圈和QQ空间的九宫格图片展示自定义控件

    NineGridLayout 一个仿微信朋友圈和QQ空间的九宫格图片展示自定义控件. GitHub:https://github.com/HMY314/NineGridLayout 一.介绍 1.当只 ...

  6. python 新模块或者包的安装方法

    主要介绍通过pip自动工具来安装需要的包. 1,先安装pip 下载pip的包(包括setup.py文件) cmd载入到pip本地文件所在路径,使用命令进行安装. python setup.py ins ...

  7. WebService 的Description 属性说明(转)

    转自:http://exception.thinksaas.cn/0/173/173623.html 在WebMethod的description 中可使用超文本, 举例: 如上图中,红框类的WebS ...

  8. BZOJ3626: [LNOI2014]LCA(树链剖分+线段树)

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q ...

  9. TC快速搜索在win10下不可用

    今天突然发现TC的快速搜索在win10下突然不可用,按Ctrl + s 呼出快速搜索栏后半天不响应也无法输入文字.论坛里给出来的建议是将 QuickSearch 2.2.3 升级到 2.2.6,目前插 ...

  10. Linux登录状态

    1.hostname 显示主机名称.设置主机名称 1)显示主机名称 xiaohuang@xiaohuang-virtual-machine:~$ hostname xiaohuang-virtual- ...