添加每天重复的闹钟,更改日期为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处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

MySQL重复数据处理的七种高效方法

《MySQL重复数据处理的七种高效方法》你是不是也曾遇到过这样的烦恼:明明系统测试时一切正常,上线后却频频出现重复数据,大批量导数据时,总有那么几条不听话的记录导致整个事务莫名回滚,今天,我就跟大家分... 目录1. 重复数据插入问题分析1.1 问题本质1.2 常见场景图2. 基础解决方案:使用异常捕获3.

CentOS7更改默认SSH端口与配置指南

《CentOS7更改默认SSH端口与配置指南》SSH是Linux服务器远程管理的核心工具,其默认监听端口为22,由于端口22众所周知,这也使得服务器容易受到自动化扫描和暴力破解攻击,本文将系统性地介绍... 目录引言为什么要更改 SSH 默认端口?步骤详解:如何更改 Centos 7 的 SSH 默认端口1

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

Mysql表如何按照日期字段的年月分区

《Mysql表如何按照日期字段的年月分区》:本文主要介绍Mysql表如何按照日期字段的年月分区的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、创键表时直接设置分区二、已有表分区1、分区的前置条件2、分区操作三、验证四、注意总结一、创键表时直接设置分区

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 进行