在我的应用程序中,我有一个功能,可以在4个senerios中触发警报:
>只有一次用户选择的日期和时间
>每日选择的时间
>每周根据所选日期和时间
>用户选择的一周中的自定义日期
我通过以下方式成功实施了前3个senerios:
只有一次:
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);
对于用户选择的自定义工作日(例如,仅适用于星期一和星期五,每周重复一次)我使用的是与迭代时每周调度相同的代码.但它不适用于星期一(星期五之前设定)和星期五工作.此外,如果今天(系统日期)是星期一或星期五,它不会触发今天的警报.
那么我如何实现每周自定义日的每周警报计划?
解决方法:
您无法告诉警报管理器您想要触发的日期.
一种解决方案是针对您希望每周重复触发一周的每一天发出警报.
因此,对于您的周一和周五场景,您将在周一设置每周重复提醒,并在周五设置每周重复提醒.
示例代码:
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全部内容,希望文章能够帮你解决重复警报周特定日期android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)