AlarmManager的使用机制有的称呼为全局定时器,有的称呼为闹钟。指定时长或以周期形式执行某项操作。通过对它的使用,个人觉得叫全局定时器比较合适,其实它的作用和Timer有点相似。都有两种相似的用法:(1)在指定时长后执行某项操作(2)周期性的执行某项操作
AlarmManager对象配合Intent使用,可以定时的开启一个Activity, 发送一个BroadCast,或者开启一个Service,run一段代码。概括起来就是通过Intent对组件进行操作。
下面的代码详细的介绍了两种定时方式的使用:
(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);
setRepeating方法有4个参数,这些参数的含义如下:
type:表示警报类型,一般可以取的值是AlarmManager.RTC和AlarmManager.RTC_WAKEUP。如果将type参数值设为AlarmManager.RTC,表示是一个正常的定时器,如果将type参数值设为AlarmManager.RTC_WAKEUP,除了有定时器的功能外,还会发出警报声(例如,响铃、震动)。
triggerAtTime:第1次运行时要等待的时间,也就是执行延迟时间,单位是毫秒。
interval:表示执行的时间间隔,单位是毫秒。
operation:一个PendingIntent对象,表示到时间后要执行的操作。PendingIntent与Intent类似,可以封装Activity、BroadcastReceiver和Service。但与Intent不同的是,PendingIntent可以脱离应用程序而存在。
type:表示警报类型,一般可以取的值是AlarmManager.RTC和AlarmManager.RTC_WAKEUP。如果将type参数值设为AlarmManager.RTC,表示是一个正常的定时器,如果将type参数值设为AlarmManager.RTC_WAKEUP,除了有定时器的功能外,还会发出警报声(例如,响铃、震动)。
triggerAtTime:第1次运行时要等待的时间,也就是执行延迟时间,单位是毫秒。
interval:表示执行的时间间隔,单位是毫秒。
operation:一个PendingIntent对象,表示到时间后要执行的操作。PendingIntent与Intent类似,可以封装Activity、BroadcastReceiver和Service。但与Intent不同的是,PendingIntent可以脱离应用程序而存在。
最后再介绍一下type参数:
public static final int ELAPSED_REALTIME
//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3 (0x00000003)。
public static final int ELAPSED_REALTIME_WAKEUP
//能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。
public static final int RTC
//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用 System.currentTimeMillis()获得。系统值是1 (0x00000001) 。
public static final int RTC_WAKEUP
//能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。
//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3 (0x00000003)。
public static final int ELAPSED_REALTIME_WAKEUP
//能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。
public static final int RTC
//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用 System.currentTimeMillis()获得。系统值是1 (0x00000001) 。
public static final int RTC_WAKEUP
//能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。
本文深入解析Android AlarmManager的使用机制,详细介绍了如何通过AlarmManager配合Intent实现指定时长后执行操作及周期性执行任务,并提供了具体的代码实例。重点阐述了两种定时方式的使用场景与区别,同时解释了如何通过AlarmManager的set和cancel方法来控制定时任务的启动与停止。
7231

被折叠的 条评论
为什么被折叠?



