Android-AlarmManager(全局定时器/闹钟)

仿Siri的中文语音助理源码
http://www.eoeandroid.com/thread-203444-1-1.html
 
AlarmManager的使用机制有的称呼为全局定时器,有的称呼为闹钟。通过对它的使用,个人觉得叫全局定时器比较合适,其实它的作用和Timer有点相似。都有两种相似的用法:(1)在指定时长后执行某项操作(2)周期性的执行某项操作
AlarmManager对象配合Intent使用,可以定时的开启一个Activity,发送一个BroadCast,或者开启一个Service.
下面的代码详细的介绍了两种定时方式的使用:
(1)在指定时长后执行某项操作
复制代码
  //操作:发送一个广播,广播接收后Toast提示定时操作完成 
     Intent intent =new Intent(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();
复制代码

注意:receiver记得在manifest.xml注册

复制代码
  public static class alarmreceiver extends BroadcastReceiver{ 
  
        @Override
        public void onReceive(Context context, Intent intent) { 
            // TODO Auto-generated method stub 
            if(intent.getAction().equals("short")){ 
                Toast.makeText(context, "short alarm", Toast.LENGTH_LONG).show(); 
            }else{ 
                Toast.makeText(context, "repeating alarm",  
                      Toast.LENGTH_LONG).show(); 
            } 
        } 
    }
复制代码

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

复制代码
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的setRepeating()相当于Timer的Schedule(task,delay,peroid);有点差异的地方时Timer这个方法是指定延迟多长时间以后开始周期性的执行task;AlarmManager的取消:(其中需要注意的是取消的Intent必须与启动Intent保持绝对一致才能支持取消AlarmManager)

Intent intent =new Intent(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);

 

 

posted on   vus520  阅读(1203)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2012年9月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6
点击右上角即可分享
微信分享提示