添加每天重复的闹钟,更改日期为2天后再改回来,前面两天的闹钟不会响

本文主要是介绍添加每天重复的闹钟,更改日期为2天后再改回来,前面两天的闹钟不会响,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

添加每天重复的闹钟,更改日期为2天后再改回来,前面两天的闹钟不会响

[DESCRIPTION]


操作步骤:
1. 当前时间为5月30号14:00,设置闹钟为14:05,每天重复
2. 更改系统时间为6月1号,再次更改为5月30号到14:05时,闹钟不会响。
实际结果:
到14:05时,闹钟不会响。


[SOLUTION]


系统时间发生变化的时候,Alarm时间不能随之而变,导致闹钟不响应
麻烦修改文件AlarmStateManager.JAVA。修改fixAlarmInstances 为如下形式:
public static void fixAlarmInstances(Context context) {
/// M: record for duplicated instance, and delete them
HashMap<Long, AlarmInstance> duplicatedInstance = new HashMap<Long, AlarmInstance>();
// Register all instances after major time changes or when phone restarts
// TODO: Refactor this code to not use the overloaded registerInstance method.
ContentResolver contentResolver = context.getContentResolver();
for (AlarmInstance instance : AlarmInstance.getInstances(contentResolver, null)) {
/// M: record for duplicated instance, and delete them @{
if (duplicatedInstance.get(instance.mAlarmId) == null) {
duplicatedInstance.put(instance.mAlarmId, instance);
} else {
// Delete current instance
AlarmInstance.deleteInstance(contentResolver, instance.mId);
continue;
}
/// @}
/// M: Fix the alarm when time changed
instance = getFixedAlarmInstance(context, instance);
AlarmStateManager.registerInstance(context, instance, false);
}
AlarmStateManager.updateNextAlarm(context);
}
并添加方法:
/** M: Update the alarmInstances who can be set a nearly time @{ */
private static AlarmInstance getFixedAlarmInstance(Context context, AlarmInstance
instance) {
ContentResolver resolver = context.getContentResolver();
Alarm alarm = Alarm.getAlarm(resolver, instance.mAlarmId);
Calendar currentTime = Calendar.getInstance();// the system's current time
// Generate the new instance use the alarm's rule
AlarmInstance newInstance = alarm.createInstanceAfter(currentTime);
Calendar newTime = newInstance.getAlarmTime();// the new instance's time
Calendar alarmTime = instance.getAlarmTime();// the original instance's time
// If the new instance's time is before the original instance's time
// then we can use the new instance's year,month and day with original instance's
// hour and minutes, so that we can keep all the state of the alarm
if (newTime.before(alarmTime)) {
// use the new instance time, update the year,month and day
int newYear = newTime.get(Calendar.YEAR);
int newMonth = newTime.get(Calendar.MONTH);
int newDay = newTime.get(Calendar.DAY_OF_MONTH);
alarmTime.set(Calendar.YEAR, newYear);
alarmTime.set(Calendar.MONTH, newMonth);
alarmTime.set(Calendar.DAY_OF_MONTH, newDay);
// if the alarmTime is still before currentTime, then add a day
while (alarmTime.before(currentTime)) {
alarmTime.add(Calendar.DAY_OF_MONTH, 1);
}
instance.setAlarmTime(alarmTime);
AlarmInstance.updateInstance(resolver, instance);
}
return instance;
}
/** @} */


这篇关于添加每天重复的闹钟,更改日期为2天后再改回来,前面两天的闹钟不会响的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/300844

相关文章

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

springboot日期格式化全局LocalDateTime详解

《springboot日期格式化全局LocalDateTime详解》文章主要分析了SpringBoot中ObjectMapper对象的序列化和反序列化过程,并具体探讨了日期格式化问题,通过分析Spri... 目录分析ObjectMapper与jsonSerializer结论自定义日期格式(全局)扩展利用配置

Redis 多规则限流和防重复提交方案实现小结

《Redis多规则限流和防重复提交方案实现小结》本文主要介绍了Redis多规则限流和防重复提交方案实现小结,包括使用String结构和Zset结构来记录用户IP的访问次数,具有一定的参考价值,感兴趣... 目录一:使用 String 结构记录固定时间段内某用户 IP 访问某接口的次数二:使用 Zset 进行

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

Oracle数据库使用 listagg去重删除重复数据的方法汇总

《Oracle数据库使用listagg去重删除重复数据的方法汇总》文章介绍了在Oracle数据库中使用LISTAGG和XMLAGG函数进行字符串聚合并去重的方法,包括去重聚合、使用XML解析和CLO... 目录案例表第一种:使用wm_concat() + distinct去重聚合第二种:使用listagg,

MySQL中删除重复数据SQL的三种写法

《MySQL中删除重复数据SQL的三种写法》:本文主要介绍MySQL中删除重复数据SQL的三种写法,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下... 目录方法一:使用 left join + 子查询删除重复数据(推荐)方法二:创建临时表(需分多步执行,逻辑清晰,但会

更改docker默认数据目录的方法步骤

《更改docker默认数据目录的方法步骤》本文主要介绍了更改docker默认数据目录的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1.查看docker是否存在并停止该服务2.挂载镜像并安装rsync便于备份3.取消挂载备份和迁