在一周中的特定日期重复闹钟android

编程入门 行业动态 更新时间:2024-10-25 02:20:38
本文介绍了在一周中的特定日期重复闹钟android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的应用程序中,我有一个功能可以在 4 种情况下触发警报:

In my application I have a functionality to trigger alarm in 4 senerios:

  • 对于用户选择的日期和时间仅一次
  • 每天选择的时间
  • 每周根据选择的日期和时间
  • 用户选择的自定义星期几

我使用以下方法成功实现了前 3 个 senerios:

I successfully implement the first 3 senerios by using the follow:

仅一次:

Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, Integer.parseInt(date[0])); calendar.set(Calendar.MONTH, (Integer.parseInt(date[1])) - 1); calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[2])); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0])); calendar.set(Calendar.MINUTE, Integer.parseInt(time[1])); calendar.set(Calendar.SECOND, 0); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

对于每日日程安排:

Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0])); calendar.set(Calendar.MINUTE, Integer.parseInt(time[1])); calendar.set(Calendar.SECOND, 0); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

对于每周安排(根据系统日期):

Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0])); calendar.set(Calendar.MINUTE, Integer.parseInt(time[1])); calendar.set(Calendar.SECOND, 0); //long interval = calendar.getTimeInMillis() + 604800000L; alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);

对于用户选择的自定义工作日(例如,仅适用于星期一和星期五,每周重复一次),我使用的代码与用于每周迭代调度的代码相同.但它不适用于星期一(设置在星期五之前)和星期五工作.此外,如果今天(系统日期)是星期一或星期五,则不会触发今天的警报.

For custom weekdays chosen by user (ex. only for monday and friday, repeated weekly) I am using the same code that I used for weekly scheduling by iteration. But its not working for monday (which is set before friday) and working for friday. Also, it does not trigger the alarm for today if today (system date) is a monday or a friday.

那么我如何为一周中的自定义日期实施每周警报计划?

推荐答案

您无法告诉警报管理器您希望它在哪一天触发.

There isn't a way for you to tell Alarm manager which days you want to it trigger.

一种解决方案是为您希望它每周重复触发的一周中的每一天设置一个警报.

One solution would be to have an alarm for each day of the week you want it to trigger repeating weekly.

因此,对于周一和周五的情景,您将在周一设置每周重复提醒,并在周五设置每周重复提醒.

So for your Monday and Friday scenario, you would set a weekly repeating reminder on Monday and a weekly repeating reminder on Friday.

示例代码:

private void scheduleAlarm(int dayOfWeek) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek); // Check we aren't setting it in the past which would trigger it to fire instantly if(calendar.getTimeInMillis() < System.currentTimeMillis()) { calendar.add(Calendar.DAY_OF_YEAR, 7); } // Set this to whatever you were planning to do at the given time PendingIntent yourIntent; AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, yourIntent); } private void setUpAlarms() { scheduleAlarm(Calendar.MONDAY); scheduleAlarm(Calendar.FRIDAY); }

更多推荐

在一周中的特定日期重复闹钟android

本文发布于:2023-11-27 05:10:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:闹钟   日期   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!