简体   繁体   English

AlarmManager警报未在分钟的变化中完全启动

[英]AlarmManager alarm not starting exactly at the change of minute

I am doing an alarm clock and although using exact alarms it is not starting at the change of minute: 我正在制作一个闹钟,尽管使用了精确的闹钟,但它并不是在一分钟的变化开始的:

For example I set is at 7:30 it will start somewhere between 7:30:00 - 7:31 (have not been able to check exact time). 例如,我设置为7:30,它将在7:30:00-7:31之间开始(无法检查确切时间)。

I call the alarm like this: 我这样拨打警报:

// Create the alarm
CustomAlarmManager alarmManager = new CustomAlarmManager(getActivity());
alarmManager.scheduleRepeatingAlarm(getActivity(), alarmID, alarmHour, alarmMinute);

Which runs this function to create the alarm: 运行此功能以创建警报:

public void scheduleRepeatingAlarm(Context context, int alarmID, int alarmHour, int alarmMinute) {

    // Create an intent to go to the notifications reciever class
    Intent intent = new Intent(context, AlarmNotificationReciever.class);

    // Bundle information and add to the intent for use later
    Bundle extras = new Bundle();
    extras.putInt("AlarmId", alarmID);
    extras.putInt("AlarmHour", alarmHour);
    extras.putInt("AlarmMinute", alarmMinute);
    intent.putExtras(extras);

    // Create a pending intent for the alarm id and intent
    PendingIntent pIntent = PendingIntent.getBroadcast(context,
            alarmID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    // Create a calender variable with the time for the alarm
    Calendar calender = Calendar.getInstance();
    calender.set(Calendar.HOUR_OF_DAY, alarmHour);
    calender.set(Calendar.MINUTE, alarmMinute);

    // Create a variable for the current alarm time
    LocalTime currentAlarmTime = LocalTime.parse(alarmHour+":"+alarmMinute+":"+"00");

    // Create a variable for the local time
    LocalTime localDeviceTime = LocalTime.now();

    // If the alarm has already elapsed today add one day to it so it runs for tomorrow
    if (localDeviceTime.isAfter(currentAlarmTime)) {

        calender.add(Calendar.DATE, 1);
        System.out.println("The time of day for the alarm has already past rescheduling for tomorrow");
    }

    // For the different versions of operating system set off a single shot alarm
    if(Build.VERSION.SDK_INT >= 23) {
        mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                calender.getTimeInMillis(), pIntent);
    } else if(Build.VERSION.SDK_INT >= 19) {
        mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);
    } else {
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);
    }
}

I am assuming the problem is going to be with these lines here: 我假设问题出在这里:

Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);

And this line when setting the alarm: 而在设置警报时此行:

mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);

But I am not sure why that would be not starting exactly on the change of minute. 但是我不确定为什么那不是从分钟的改变开始的。

Thanks for your help 谢谢你的帮助

Calendar.getInstance() returns a Calendar instance set to the current time (and date). Calendar.getInstance()返回设置为当前时间(和日期)的Calendar实例。

You're setting the HOUR_OF_DAY and MINUTE fields of this Calendar instance, but the SECOND field still have its initial value, that is the cause of this behaviour. 您正在设置此Calendar实例的HOUR_OF_DAYMINUTE字段,但是SECOND字段仍具有其初始值,这就是这种行为的原因。

Change the following: 更改以下内容:

Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);

To this: 对此:

Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM