android prevent immediate trigger of alarm service if alarm time has passed for the day

后端 未结 1 928
死守一世寂寞
死守一世寂寞 2020-11-22 14:38

The reference for Alarm Manager says that

If the stated trigger time is in the past, the alarm will be triggered immediately.

相关标签:
1条回答
  • 2020-11-22 15:08

    You don't need to create Timestamps. You can do it with your Calendar.

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calendar.set(Calendar.MINUTE, minute);
    
    if(calendar.before(Calendar.getInstance())) {
        calendar.add(Calendar.DATE, 1);
    }
    
    alarmManager.set(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(), pendingDinnerIntent);
    

    I would also mention that as of KitKat, if your targetSdkVersion is 19 or above, the AlarmManager#set() method is not exact. If you want your alarm to fire at an exact time, you'll need to use a setExact*() method.

    0 讨论(0)
提交回复
热议问题