首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在Android中设置多个告警触发不同的通知

在Android中设置多个告警触发不同的通知,可以通过以下步骤实现:

  1. 创建多个AlarmManager对象:AlarmManager是Android提供的用于设置定时任务的类。可以通过调用Context.getSystemService(Context.ALARM_SERVICE)方法获取AlarmManager对象。
  2. 创建不同的PendingIntent:PendingIntent是用于封装即将执行的Intent的对象。可以通过调用PendingIntent.getActivity()PendingIntent.getService()PendingIntent.getBroadcast()方法创建不同的PendingIntent。
  3. 设置不同的触发时间:通过调用AlarmManager的set()setExact()方法,传入不同的触发时间和对应的PendingIntent,来设置多个告警。

以下是一个示例代码:

代码语言:txt
复制
// 创建AlarmManager对象
AlarmManager alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
AlarmManager alarmManager2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

// 创建不同的PendingIntent
Intent intent1 = new Intent(this, AlarmReceiver.class);
Intent intent2 = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0, intent1, 0);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1, intent2, 0);

// 设置触发时间
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
calendar1.set(Calendar.HOUR_OF_DAY, 8);
calendar1.set(Calendar.MINUTE, 0);
calendar1.set(Calendar.SECOND, 0);
alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent1);

Calendar calendar2 = Calendar.getInstance();
calendar2.setTimeInMillis(System.currentTimeMillis());
calendar2.set(Calendar.HOUR_OF_DAY, 12);
calendar2.set(Calendar.MINUTE, 0);
calendar2.set(Calendar.SECOND, 0);
alarmManager2.set(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), pendingIntent2);

上述代码中,创建了两个AlarmManager对象和两个不同的PendingIntent对象。分别设置了两个触发时间,并通过set()方法将触发时间和对应的PendingIntent传入,实现了在Android中设置多个告警触发不同的通知。

注意:为了接收到告警触发的通知,需要创建一个继承自BroadcastReceiver的AlarmReceiver类,并在AndroidManifest.xml文件中注册该类。在AlarmReceiver类中可以编写相应的逻辑来处理触发的通知。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

Android开发笔记(五十)定时器AlarmManager

Java中的定时器机制有现成的方案,就是Timer+TimerTask。其中TimerTask用来描述时刻到达后的事务处理,而Timer用来调度定时任务,如何时启动、间隔多久再次运行等等。 Timer的调度方法是schedule,主要有三个参数。第一个参数表示用来调度的定时任务,第二个参数表示延迟多久首次启动任务,第三个参数表示间隔多久再次启动任务。 public void schedule(TimerTask task, long delay, long period) 定时任务得自己写个继承自TimerTask的新类,并重写run方法填入具体的事务处理代码。调用Timer的schedule方法,定时任务便会按照调度设置按时启动;TimerTask不能直接设置运行的次数上限,一旦启动就会持续定时运行,除非对象销毁或者调用了TimerTask的cancel方法。调用cancel方法停止定时任务后,若想重启该定时任务,只能重新声明TimerTask对象,并且重新调用schedule方法。 Timer+TimerTask的实质是利用开启Thread来触发定时任务,所以TimerTask实际上运行于非UI线程,也就无法直接操作UI。若想在TimerTask中修改UI控件,得通过Handler发送消息来间接实现。

01

Android开发笔记(一百六十)休眠模式下的定时器控制

定时器AlarmManager常常用于需要周期性处理的场合,比如闹钟提醒、任务轮询等等。并且定时器来源于系统服务,即使App已经不在运行了,也能收到定时器发出的广播而被唤醒。似此回光返照的神技,便遭到开发者的滥用,造成用户手机充斥着各种杀不光进程,就算通过手机安全工具一再地清理内存,只要定时设定的时刻到达,刚杀掉的流氓App就会死灰复燃。长此以往,手机的运行速度越来越慢,内存也越来越不够用了,更糟糕的是,电量消耗地越来越快。 Android手机越用越慢的毛病老大不掉,为此每次系统版本升级,Android都力图在稳定性、安全性上有所改善。针对定时器AlarmManager的滥用问题,Android从4.4开始,修改了setRepeating方法的运行规则。原本该方法可指定每隔固定时间就发送定时广播,但在Android4.4之后,操作系统为了节能省电,将会自动调整定时器唤醒的时间。比如原来调用setRepeating方法设定了每隔10秒发送广播,但App在实际运行过程中,很可能过了好几分钟才发送一次广播,这意味着该方法将不再保证每次工作都在开发者设置的时间开始。 正如博文《Android开发笔记(七十五)内存泄漏的处理》描述的那样,当时为了演示定时器发生内存泄漏的场景,并没有直接调用setRepeating方法,而是接力调用set方法。App每次收到定时广播之后,还得重新开始下一次的定时任务,如此方可兼容Android4.4之后的持续定时功能。下面是将setRepeating方法改为使用set方法实现的代码例子:

02
领券