添加每天重复的闹钟,更改日期为2天后再改回来,前面两天的闹钟不会响
时间:10-02
整理:3721RD
点击:
[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;
}
/** @} */
操作步骤:
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;
}
/** @} */