Android中alarmManager的使用

本文分享了如何在Android项目中利用AlarmManager类实现定时提醒功能,包括定义和设置广播Intent、使用PendingIntent代理类、设置闹钟类型与时间参数、以及通过AlarmManager的常用方法来实现一次性或重复性闹钟。详细介绍了如何根据不同闹钟类型选择合适的闹钟间隔时间单位,并提供了实际应用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AlarmManager使用

最近项目中定时提醒的功能用到了AlarmManger,在这和大家分享一下:

1.AlarmManager这个类提供对系统闹钟服务的访问接口,是android中比较常用的系统级提示服务。
   使用闹钟服务,会在特定的时间为我们广播一个我们设定好的Intent,我们可以通过Intent执行startActivity,startService和senBroadcast。

在AlarmManager中我们使用的是PendingIntent,PendingIntent可以理解为Intent的代理类。

2.定义并设置一个广播

Intent intent = new Intent(this,DemoReceiver.class);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this,100,intent,Intent.FLAG_ACTIVITY_NEW_TASK));
AlarmManager mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  //获取AlarmManager对象
mAlarmManager.set(AlarmManager.RTC_WAKEUP, setMills, mPendingIntent);  //设置闹钟服务


3.AlarmManager中几个常用的方法:
(1)set(int type,long triggerAtTime, PendingIntent operation);
此方法用于设置一次性闹钟,type闹钟类型,triggerAtTime闹钟执行时间,operation就是我们定义的PendingIntent。
如果已经有了一个相同的闹钟被设置过了,那么前一个闹钟将会被新设置的闹钟所替代。

(2)setRepeating(int type,long triggerAtTime, long interval,PendingIntent operation);
用于设置可重复的闹钟,type闹钟类型,triggerAtTime闹钟首次执行的时间,interval重复闹钟的时间间隔,operation就是我们定义的PendingIntent。

(3)setInexactRepeating(int type,long triggerAtTime, long interval,PendingIntent operation);
  该方法也用于设置重复闹钟,和setRepeating方法相似,但是此方法中的闹钟间隔时间不是固定的。

(4)cancel(PendingIntent operation)取消闹钟设置,取消闹钟时PendingIntent中的Intent必须和设置闹钟时的Intent一致才行。

4.设置闹钟时的type参数的常用值,主要区分在睡眠状态唤醒和睡眠状态下不唤醒,相对时间和绝对时间
AlarmManager.ELAPSED_REALTIME 设置此种类型时闹钟在手机睡眠状态下不可用,此状态下闹钟会使用相对时间(相对于系统启动开始);
AlarmManager.ELAPSED_REALTIME_WAKEUP 这种类型的状态在手机睡眠状态下也会唤醒系统并执行指定功能,使用系那个对时间
AlarmManager.RTC 表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用绝对时间,即系统当前时间。
Alarmmanager.RTC_WAKEUP 闹钟在睡眠状态下也会唤醒系统并执行指定功能,该状态下使用绝对时间。

AlarmManager.POWER_OFF_WAKEUP 闹钟在手机关机状态下也能正常执行,使用的是绝对时间,但是在某些SDK版本中不支持(具体不详)。

5.long triggerAtTime参数
首次触发闹钟的时间,以毫秒为单位,此参数和4中的type类型有密不可分的关系,如果type对应的状态下使用的是绝对时间则此参数使用的就是绝对时间,ex:System.currentTimeMillis();
如果type对应的状态下使用的是相对时间,则此参数就是使用相对时间,ex:SystemClock.elapsedRealtime();


6.PendingIntent mPendingIntent 我们指定的操作,ex:发送广播,启动应用,开启服务。
  获取PendingIntent对象时要采用不同的方式获取:
  开启广播PendingIntent.getBroadcast(Context context, int requestCode,Intent intent, int flags)的方式获取,
  开启服务PendingIntent.getService(Context context, int requestCode,Intent intent, int flags)的方式获取,
  开启Activity PendingIntent.getActivity(Context context, int requestCode,Intent intent, int flags)的方式获取,



好了,废话差不多了,看代码:
public void setAlarm(){
  //在当天的18点钟启动闹钟发送广播
Calendar mSetCalendar = Calendar.getInstance();
mSetCalendar.set(Calendar.HOUR_OF_DAY, 18);
mSetCalendar.set(Calendar.MINUTE, 0);
mSetCalendar.set(Calendar.SECOND, 0);
mSetCalendar.set(Calendar.MILLISECOND, 0);

long setMills = mSetCalendar.getTimeInMillis();

Intent intent = new Intent(this, AutoQuitReceiver.class);   //广播
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 200,
intent, Intent.FLAG_ACTIVITY_NEW_TASK);

AlarmManager mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  //系统服务

//发送一次性广播,手机睡眠状态下也会执行
mAlarmManager.set(AlarmManager.RTC_WAKEUP, setMills, mPendingIntent);  

//发送重复广播,第一次在18点执行,以后每隔5分钟执行一次。
//mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, setMills, 1000*60*5, mPendingIntent);

}

public class AutoQuitReceiver extends BroadcastReceiver {  //BroadcastReceiver别忘了在AndroidManifest.xml中注册

@Override
publicvoid onReceive(Context context, Intent intent) {
Toast.makeText(context, "闹钟启动了...", Toast.LENGTH_SHORT).show();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值