Android AlarmManager的一些问题

AlarmManager的问题: app

我开始的代码是这样写的 学习

Java代码  
  1. alarmManager.set(AlarmManager.RTC_WAKEUP, 60*1000, pendingIntent);
个人本意是设定一分钟后启动pendingIntent 可是每次都是我设置完闹钟以后立马就启动了。

后来我发现问题出在第二个参数上 我对他的理解是错误的 spa

type
One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP}, RTC or RTC_WAKEUP.

 

triggerAtTime
Time the alarm should first go off, using the appropriate clock (depending on the alarm type).

 

我以前觉得它是“延迟”时间,而实际它是“启动”时间。 orm

要理解这个参数还要看type这个参数 对象

Java代码  
  1. public static final int ELAPSED_REALTIME  
  2.         // 当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时 间,能够经过调用SystemClock.elapsedRealtime()得到。系统值是3    (0x00000003)。  
  3.   
  4.         public static final int ELAPSED_REALTIME_WAKEUP  
  5.         //能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。  
  6.   
  7.         public static final int RTC  
  8.         //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,能够经过调用 System.currentTimeMillis()得到。系统值是1 (0x00000001) 。  
  9.         public static final int RTC_WAKEUP  
  10.         //能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。  

它大体分为两种类型 一种是相对时间 一种是绝对时间 get

因此,根据使用的类型不一样 triggerAtTime设置也有所不一样 it

若是使用ELAPSED_REALTIME_WAKEUP类型 应该调用SystemClock.elapsedRealtime()获取相对时间在加上你设定的延迟时间 io

Java代码  
  1. alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+5000, sender);  

若是使用RTC_WAKEUP类型 应该调用System.currentTimeMillis()获取从1970.1.1号以来的时间在加上你设定的延迟时间 table

Java代码  
  1. alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, sender);  

============================================= ast

setRepeating方法有4个参数,这些参数的含义以下:

type: 表示警报类型,通常能够取的值是AlarmManager.RTCAlarmManager.RTC_WAKEUP。若是将type参数值设为 AlarmManager.RTC,表示是一个正常的定时器,若是将type参数值设为AlarmManager.RTC_WAKEUP,除了有定时器的 功能外,还会发出警报声(例如,响铃、震动)。

triggerAtTime:第1次运行时要等待的时间,也就是执行延迟时间,单位是毫秒。

interval:表示执行的时间间隔,单位是毫秒。

operation: 一个PendingIntent对象,表示到时间后要执行的操做。PendingIntent与Intent相似,能够封装Activity、 BroadcastReceiver和Service。但与Intent不一样的是,PendingIntent能够脱离应用程序而存在。

最近看到大神的一篇文章。学习学习……

相关文章
相关标签/搜索