AlarmManager类的应用(实现闹钟功能)

来源:互联网 发布:淘宝优选好店有假货吗 编辑:程序博客网 时间:2024/06/05 11:28
 

1、AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,可以实现从指定时间开始,以一个固定的间隔时间执行某项操作,所以常常与广播(Broadcast)连用,实现闹钟等提示功能

2、AlarmManager的常用方法有三个:

(1)set(int type,long startTime,PendingIntent pi)

该方法用于设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作。

(2)setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法用于设置重复闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟首次执行时间,第三个参数表示闹钟两次执行的间隔时间,第三个参数表示闹钟响应动作。

(3)setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法也用于设置重复闹钟,与第二个方法相似,不过其两个闹钟执行的间隔时间不是固定的而已。

3、三个方法各个参数详悉:

(1)int type:闹钟的类型,常用的有5个值:AlarmManager.ELAPSED_REALTIME、AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。

AlarmManager.ELAPSED_REALTIME表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始),状态值为3;

Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.

AlarmManager.ELAPSED_REALTIME_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间,状态值为2;

Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep), which will wake up the device when it goes off.

Constant Value: 2 (0x00000002)

AlarmManager.RTC表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间,状态值为1;

Alarm time in System.currentTimeMillis() (wall clock time in UTC). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.

Constant Value: 1 (0x00000001)

AlarmManager.RTC_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,状态值为0;

Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.

Constant Value: 0 (0x00000000)

AlarmManager.POWER_OFF_WAKEUP表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间,状态值为4;不过本状态好像受SDK版本影响,某些版本并不支持;

(2)long startTime:闹钟的第一次执行时间,以毫秒为单位,可以自定义时间,不过一般使用当前时间。需要注意的是,本属性与第一个属性(type)密切相关,如果第一个参数对应的闹钟使用的是相对时间(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那么本属性就得使用相对时间(相对于系统启动时间来说),比如当前时间就表示为:SystemClock.elapsedRealtime();如果第一个参数对应的闹钟使用的是绝对时间(RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本属性就得使用绝对时间,比如当前时间就表示为:System.currentTimeMillis()。

(3)long intervalTime:对于后两个方法来说,存在本属性,表示两次闹钟执行的间隔时间,也是以毫秒为单位。

(4)PendingIntent pi:是闹钟的执行动作,比如发送一个广播、给出提示等等。PendingIntent是Intent的封装类。需要注意的是,如果是通过启动服务来实现闹钟提示的话,PendingIntent对象的获取就应该采用Pending.getService(Context c,int i,Intent intent,int j)方法;如果是通过广播来实现闹钟提示的话,PendingIntent对象的获取就应该采用PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法;如果是采用Activity的方式来实现闹钟提示的话,PendingIntent对象的获取就应该采用PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。如果这三种方法错用了的话,虽然不会报错,但是看不到闹钟提示效果。

4、 AlarmManager使用示例:利用用户自定义广播实现闹钟功能,从当前时间开始,每隔10分钟提示一次

(1)实现原理:在SendActivity.java中定义一个AlarmManager对象,指定该对象从当前时间开始,每隔10分钟向名为“MYALARMRECEIVER”的广播接收器发出一条广播,附加消息内容为“你该打酱油了”;创建一个名为MyReceiver的广播接收器,在其onReceive方法中获取Intent对象传过来的值(“你该打酱油了”)并用一个Toast组件显示出来;在AndroidManifest.xml文件中注册SendActivity类和广播接收器类MyReceiver,设置MyReceiver的action的值为“MYALARMRECEIVER”

(2)代码实现:

第一步:创建广播接收类MyReceiver.java,在其onReceive方法中获取Intent的附加信息msg,并用Toast组件显示

[java] view plaincopy
  1. public void onReceive(Context context,Intent intent){  
  2.      String msg = intent.getStringExtra("msg");  
  3.      Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();  
  4. }  

第二步:在AndroidManifest.xml中注册广播接收类MyReceiver.java,设置其action值为“MYALARMRECEIVER”

[java] view plaincopy
  1. <receiver android:name=".MyReceiver">  
  2.    <intent-filter>  
  3.       <action android:name="MYALARMRECEIVER" />  
  4.    </intent-filter>  
  5. </receiver>  

第三步:创建SendActivity.java,用于设置闹钟,定时发出广播

[java] view plaincopy
  1. //创建Intent对象,action指向广播接收类,附加信息为字符串“你该打酱油了”  
  2.   
  3. Intent intent = new Intent("MYALARMRECEIVER");  
  4.   
  5. intent.putExtra("msg","你该打酱油了");  
  6.   
  7. //创建PendingIntent对象封装Intent,由于是使用广播,注意使用getBroadcast方法  
  8.   
  9. PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);  
  10.   
  11. //获取AlarmManager对象  
  12.   
  13. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);  
  14.   
  15. //设置闹钟从当前时间开始,每隔10分钟执行一次PendingIntent对象,注意第一个参数与第二个参数的关系  
  16.   
  17. am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentMillis(),600*1000,pi);  

第四步:在AndroidManifest中为SendActivity.java注册

[java] view plaincopy
  1. <activity android:name=".SendActivity" />  

同时注意: The alarm is an intent broadcast that goes to a broadcast receiver that you registered with registerReceiver(BroadcastReceiver, IntentFilter) or through the <receiver> tag in an AndroidManifest.xml file.
 
【注意1】:
If the time occurs in the past, the alarm will be triggered immediately.
这说明,但设置的时间比现在的时间还小时,就会立马执行。这在我的定时程序中用set()时也体现了。
【注意2】:
If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.
这说明不能有多个闹钟?
原创粉丝点击