通过预设时间来启动Service 中的Notification。这里我们来介绍一下全局定时器AlarmManager:
AlarmManager提供的方法:
❑ void set(int type, long triggerAtTime, PendingIntent operation)
设置一个闹钟
❑ void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
设置一个会重复的闹钟
❑ void setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
设置一个重复闹钟的不精确版本,它相对而言更节能(power-efficient)一些,因为系统可能会将几个差不多的闹钟合并为一个来执行,减少设备的唤醒次数。
内置的几个interval为:
INTERVAL_FIFTEEN_MINUTES
INTERVAL_HALF_HOUR
INTERVAL_HOUR
INTERVAL_HALF_DAY
INTERVAL_DAY
如果你将其设为DAY,那么可能这一天中的所有闹钟都会被合并掉。
❑ void cancel(PendingIntent operation)
取消一个设置的闹钟
❑ void setTimeZone(String timeZone)
设置系统的默认时区。需要android.permission.SET_TIME_ZONE
下面介绍一下代码:
Activity的 代码接下来就是Service代码
这里面的Mm()方法最后面是放入onStart()方法里面去运行的,如果放入oncreate方法里面运行该方法的话,那么提醒的消息只能运行一次,这个的原因在于启动式启动的Service每次启动一个已经启动的Service,它只有第一次运行Service时回调oncreate方法,但是你每次启动都会调用onstart方法,故如果你把方法放在oncreate里面的话 它只能启动一次该方法。源代码下载:http://download.csdn.net/detail/kluing/7398389