AlarmManager fires alarms at wrong time
For api levels below 19 you should use AlarmManager.setRepeating()
and your alarms will trigger exactly at specified time. Thou on api levels 19 and above this will no longer work. There was change in android so that all repeating alarms are inexact. So if you would like to achieve exact repeating alarm you should schedule alarm with AlarmManager.setExact()
and then when alarm triggers do it again for next week and so on every week.
Android - Alarms sometimes fires at the wrong time using AlarmManager
For API levels <19 you should use AlarmManager.setRepeating() and your alarms will trigger exactly at specified time.
Api levels >=19 and above this no longer works. There was change in android so that all repeating alarms are inexact.
So if you would like to achieve exact repeating alarm use AlarmManager.setExact().
See this question for more info.
Edit
For your purpose (a one-off alarm, at a precise time) use alarmManager.setExact(....). See docs
AlarmManager alarm goes off at the wrong time
Your are setting setInexactRepeating()
which as its name says, it could not be fired at the exact time you specified. This is an optimization so the Operating System tries to get more than one alarm which should fire at similar times to fire all of them at the same time.
Instead, you can use void setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)
. See documentation.
Example:
alarmManager.setExactAndAllowWhileIdle(android.app.AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
As its recommended, you shouldn't use a repeating alarm with exact times. Instead, you can wait until the exact alarm fires and then schedule another alarm for the next day.
For API lower than 23, you can use void setExact(int type, long triggerAtMillis, PendingIntent operation)
. And for API lower than 19, you can use void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
with the expected exact firing as always.
AlarmManager fires alarm in the wrong time if timezone is different than GMT(UTC)
So, I finally got this to work. Here's the code :)
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, operationBroadcast);
AlarmManager setRepeating() fires at incorrect time
The problem is this
val firingCal:Calendar = Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY,8)
set(Calendar.MINUTE,0)
set(Calendar.SECOND,0)
}
You are setting the time to 8:00, but if it is now past 8:00am, this is now set to the past, so the alarm fires immediately.
You have to check if the time is past 8:00 and add 1 to your day:
val firingCal:Calendar = Calendar.getInstance().apply {
if (get(Calendar.HOUR_OF_DAY) >= 8) {
add(Calendar.DAY_OF_MONTH, 1)
}
set(Calendar.HOUR_OF_DAY,8)
set(Calendar.MINUTE,0)
set(Calendar.SECOND,0)
}
Android : AlarmManager fires off for past time
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.
alarm manager sometimes firing at wrong times
Manager.prayerAlarmManager.set
is not set exact on the specified time on api 19 and above. That is probably why "some" users complain.
Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.
On api 19 and above you need setExact
to schedule at a specific time
You will need something like this:
if(android.os.Build.VERSION.SDK_INT >= 19) {
// setExact
}
else {
//set
}
Related Topics
Intercept Back Button from Soft Keyboard
Googleservice Failed to Initialize
How to Play Videos in Android from Assets Folder or Raw Folder
Date and Time Change Listener in Android
Couldn't Load Memtrack Module Logcat Error
Tabwidget Current Tab Bottom Line Color
Google Map Signed API Key Errors in Android
An Established Connection Was Aborted by the Software in Your Host MAChine
Bad Class File Magic or Version
Importing Google-Play-Service Library Showing a Red X Next to This Reference Android
Error:Execution Failed for Task ':Android:Transformclassesandresourceswithproguardforrelease'
Dynamically Add and Remove View to Viewpager
Android: How to Use Onbackpressed() with Toast
How to Find and Run the Keytool
How to Extract a List of Objects from Firebase Datasnapshot on Android