为什么我的闹钟比我设定的时间早?
Why my Alarm is fired earlier than my set time?
我目前正在尝试制作一个闹钟应用程序,它会在用户定义的日期和时间将所有音量静音。但是,警报似乎没有在正确的时间触发。我发现警报总是提前触发。
这是我设置的闹钟代码:
public static void setAlarm(Context context, String time, int day, int reqCode) {
String[] timeSplit = time.split(":");
Calendar calendar = Calendar.getInstance();
int hour = Integer.parseInt(timeSplit[0]);
int minute = Integer.parseInt(timeSplit[1]);
int days = (day-calendar.get(Calendar.DAY_OF_WEEK) + calendar.get(Calendar.DAY_OF_MONTH));
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
days,
hour,
minute,
0
);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, RuleAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, reqCode, intent, 0);
// Set the alarm to be fired every week on the selected day
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
Toast.makeText(context, "Alarm is set", Toast.LENGTH_SHORT).show();
}
我尝试将 setRepeating()
更改为 setInexactRepeating()
但没有任何改变。
试试这个
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
问题是,如果您正在为过去的时间创建 PendingIntent
警报,它将被触发 immediately.If 您想要防止这种情况发生,那么就不要设置过去的警报触发时间。使用BroadcastReceiver
接收报警。
更新:
检查我们没有将它设置在过去,这会触发它立即触发
if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 7);
}
AlarmManger 无法通过组合唤醒呼叫来节省电量。
https://developer.android.com/reference/android/app/AlarmManager.html
终于找到问题所在了。它位于 day
参数上。我传递给 day
参数的是我的微调器选择的位置,它从零开始,而日历天从一开始(星期日 = 1,星期一 = 2,依此类推)。所以,闹钟总会定在过去的日子里。我在 day
参数上加了一个,它成功了。
这是我的一段代码,其中 startAlarm()
函数被调用。
String ruleStartTime = rule_startTime.getText().toString();
// Spinner index starts from zero,
// to match with the Calendar day indexing, we need to plus it one
int dayIdx = day.getSelectedItemPosition()+1;
Alarm.setAlarm(getApplicationContext(), ruleStartTime,
dayIdx, 0);
我目前正在尝试制作一个闹钟应用程序,它会在用户定义的日期和时间将所有音量静音。但是,警报似乎没有在正确的时间触发。我发现警报总是提前触发。
这是我设置的闹钟代码:
public static void setAlarm(Context context, String time, int day, int reqCode) {
String[] timeSplit = time.split(":");
Calendar calendar = Calendar.getInstance();
int hour = Integer.parseInt(timeSplit[0]);
int minute = Integer.parseInt(timeSplit[1]);
int days = (day-calendar.get(Calendar.DAY_OF_WEEK) + calendar.get(Calendar.DAY_OF_MONTH));
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
days,
hour,
minute,
0
);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, RuleAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, reqCode, intent, 0);
// Set the alarm to be fired every week on the selected day
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
Toast.makeText(context, "Alarm is set", Toast.LENGTH_SHORT).show();
}
我尝试将 setRepeating()
更改为 setInexactRepeating()
但没有任何改变。
试试这个
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
问题是,如果您正在为过去的时间创建 PendingIntent
警报,它将被触发 immediately.If 您想要防止这种情况发生,那么就不要设置过去的警报触发时间。使用BroadcastReceiver
接收报警。
更新:
检查我们没有将它设置在过去,这会触发它立即触发
if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 7);
}
AlarmManger 无法通过组合唤醒呼叫来节省电量。 https://developer.android.com/reference/android/app/AlarmManager.html
终于找到问题所在了。它位于 day
参数上。我传递给 day
参数的是我的微调器选择的位置,它从零开始,而日历天从一开始(星期日 = 1,星期一 = 2,依此类推)。所以,闹钟总会定在过去的日子里。我在 day
参数上加了一个,它成功了。
这是我的一段代码,其中 startAlarm()
函数被调用。
String ruleStartTime = rule_startTime.getText().toString();
// Spinner index starts from zero,
// to match with the Calendar day indexing, we need to plus it one
int dayIdx = day.getSelectedItemPosition()+1;
Alarm.setAlarm(getApplicationContext(), ruleStartTime,
dayIdx, 0);