android prevent immediate trigger of alarm service if alarm time has passed for the day
You don't need to create Timestamp
s. 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.
AlarmManager alarm is called immediately when trigger time is in the past
This is the expected behavior.
From the documentation of setRepeating()
(and other AlarmManager
set methods):
If the stated trigger time is in the past, the alarm will be triggered
immediately
If you would like to prevent that happening, then simply do not set alarms with a past trigger time (e.g. check against System.currentTimeMillis()
when setting the alarm).
How to prevent alarm from running the passed time on starting
I suggest you to use to calendar.add(Calendar.DAY_OF_YEAR, 1);
Study following code:
Calendar calendar = Calendar.getInstance();
// 9 AM
calendar.add(Calendar.DAY_OF_YEAR, 1); ///to avoid firing the alarm immediately
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
Original Source
How to Prevent Past Alarms Going of Immediately
The Android alarm service will trigger every alarm that his programmed time is less than the current time (i.e. it will trigger immediately the alarms that are late). The method "getInstance" of the Calendar class returns you a calendar instance that has the current time. If you are only setting the hours, minutes and seconds of the calendar instance, then it will use the current day, so the alarm will be trigger immediately because the time has just passed. If you want to program for tomorrow an alarm if the selected time is less than the current, then you will need to verify if the selected hour is less than the current and conditionally add one day to the calendar (or sum to the millis result "24*60*60*1000" which is a day in milliseconds). Hope this helps.
Android Alarm firing instantly when I set alarm on earlier hour or after midnight
Of course it does... It's meant to work so.
Android recognizes that the time is past, so it will fire the alarm, even if it's late.
You can make sure that the time set for the alarm is after the current time. Just calculate this difference:
int diff = Calendar.getInstance().getTimeInMilis() - targetCal.getTimeInMillis();
If diff is greater than 0, then add a day to your calendar (targetCal)
Now, your device's time will be earlier (instead of being later) than the next scheduled alarm time.
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);
Related Topics
Recyclerview Slow Scrolling Performance and Lag/Halt Sometime While Scrolling
How to Close All Activities and Exit from an App
How to Convert Android.Media.Image to Bitmap Object
Android Push Notifications Not Being Received When App Closed
Android.Support.V7.App.Activitycompat Not Found
How to Convert Image to Zpl Code for Printing Using Zebra Printer
How to Emulate Gps Location in the Android Emulator
How to Select a Video from the Gallery and Get It's Real Path
Android Scrollview Not Scrolling Down After Keyboard Opens
Android Refresh a Fragment List from Its Parent Activity
How to Make Primary Key as Autoincrement for Room Persistence Lib
Failed to Resolve: Android.Support.Design:28.0.0-Rc02
How to Turn a Portable Sd Card into Internal Storage Via Adb Command
Strange Outofmemory Issue While Loading an Image to a Bitmap Object
Onmeasure Custom View Explanation