setExactAndAllowWhileIdle is almost triggered exactly - android

I searched the Internet for an answer but have not found anything.
I wrote an app where every 17 minute an SMS must be sent ( every 17 minute means: 10:17:00 and not at 10:17:49 )
Therefore, I use an AlarmManager with setExactAndAllowWhileIdle, and AlarmManager.RTC_WAKEUP.
Long whenToFireOff = treeMapInstance.getFirstEntryInTreeMap().getKey();
Calendar alarmCal = Calendar.getInstance();
alarmCal.set(Calendar.HOUR_OF_DAY, treeMapInstance.convertMillisecondsToHour(whenToFireOff));
alarmCal.set(Calendar.MINUTE, treeMapInstance.convertMillisecondsToMinutes(whenToFireOff));
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmCal.getTimeInMillis(), pi);
However, it gets always triggered round about 10:17:49, and the next scheduled alarm also at 10:34:49, and so on.
I calculated my next alarm correctly. I am absolutely 100% sure :)
Is setExactAndAllowWhileIdle not as exactly as it is written in the documentation?
Thanks

Related

How far (and why!) do Android alarms execute in the past?

I have had several frustrations with setting alarms in Android. I have tried setting repeating/non-repeating alarms and exact/inexact alarms but it does not matter, if the alarm is ever set for a time in the past, it executes as soon as it is set. I have tested this as far back as setting an alarm for 5 hours in the past and is still executes immediately.
For example:
The time is 7 AM and I set an alarm to execute at 2 AM. This is obviously meant for the next time the clock reads 2:00 AM but it does not matter, the alarm goes off at 7 AM, right after it is set.
The code below should select a random time between 1:00 AM and 3:59 AM to set/execute the alarm for the next calendar day and then the logic circles back around to set itself again after execution. The alarm will execute repeatedly, forever.
int randomHour = new Random().nextInt((3 - 1) + 1) + 1;
int randomMinute = new Random().nextInt((59 - 1) + 1) + 1;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, randomHour);
calendar.set(Calendar.MINUTE, randomMinute);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Questions:
At what point does Android stop executing alarms in the past?
Is there any way to stop this?
"This is obviously meant for the next time" Does not work for computers, they will do exactly what you tell them to do.
calendar.getTimeInMillis() returns the number of milliseconds since January 1, 1970 at 00:00:00 GMT. You need to specify not just the time but also the date that you want the alarm to go off. Instead you are always calling calendar.add(Calendar.DAY_OF_MONTH, 1); (the first day of the current month)

Setting Notification using AlarmManager by passing calendar.getTimeInMillis(),triggers notification instantly not at the time intended

I have followed the following link https://developer.android.com/training/scheduling/alarms.html.
RTC examples first one, to set an alarm for a specific time on all days. Even after following the same code, the alarm is not triggered at the time it is suppose to get triggered. Instead the notification gets triggered immediately after setting the time which is done with the help of a time picker. Following is my code snippet,
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent intentalarm = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,4);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, intentalarm);
Instead of setRepeating I also tried setInexactRepeating but there was no luck.I would also like to add that when I changed the
calendar.getTimeInMillis() and used SystemClock.elapsedRealtime() + 120 * 1000 the alarm triggered exactly after 2 minutes from the time it was set.
But when calendar.getTimeInMillis() is being used the intended working does not happen instead immediate triggering occurs.
Would be indeed very helpful if anyone can help me out find a solution.Only for a note, I could learn if alarm is set before current device time the alarm would be triggered immediately but that is not the case here.
NotificationReceiver.class is working fine as it is generated and appears on the title. But the time it appears is the cause of concern.
You are using both setTimeInMillis and hourofday and minute.
If you want your alarm to be triggered at 4:30 just add hourofday and minute.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,4);
calendar.set(Calendar.MINUTE, 30);`
I could Identify the issue I was facing. I was using a TimePicker with 24 hour format. But I used SimpleDateFormat "hh:mm". What 'h' stands for is as follows "h->Hour in am/pm (1-12) ". So any alarm that I set at AM triggers correctly. When I set a time at PM since I had used 1-12 'h' format, alarm gets set for a AM time and since that time is already passed when compared to the device time the alarm/notification got triggered immediately.The right format to use was "HH:MM" where 'H' stands for "H->Hour in day (0-23)". This resolved my issue.
Checking the TimeinMillis on online epoch time converter helped me identify this issue.Once again thanks for helping me.

Android Alarms too early

I'm using android studio to develop an application that would retrieve the time information from the database and would set an alarm to prompt the user that it's time to drink the medicine, my problem is that the alarm goes on too early
for example the retrieved time is 12:50 AM and the current time is 12:40 AM
even though there's a 10 minutes difference the alarm goes off right away
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Calendar Cnow = Calendar.getInstance();
long now=Cnow.getTimeInMillis();
Calendar time=Calendar.getInstance();
time.setTimeInMillis(0);
Date dTime=sdf.parse(strAlarmDate + " " + strTime1);
//example strAlarmDate="2015-09-27" and strTime1="12:50 AM" and now="12:40 AM" in millis
//dTime="...... 2015-09-27 00:50"
time.setTime(dTime);
//Date dNow=now.getTime();
//time.setTime(date);
//long trigger=time.getTimeInMillis()-now.getTimeInMillis();
long trigger=time.getTimeInMillis()-now;
if(trigger>0) {
ctr++;
Intent i = new Intent(Login.this, AlarmReceiver.class);
i.putExtra("message", strMedname);
i.putExtra("line", strLine);
final int _id = (int) System.currentTimeMillis();
PendingIntent pi = PendingIntent.getActivity(Login.this, _id, i, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
// am.set(AlarmManager.RTC_WAKEUP, trigger, pi);
// am.setExact(AlarmManager.RTC_WAKEUP, trigger, pi);
am.set(AlarmManager.RTC_WAKEUP, trigger, pi);
}
}catch(ParseException ex)
{
ex.printStackTrace();
}
The problem is that you set the alarm to go off at time trigger which is the time minus the current time.
You should pass time.getTimeInMillis() instead.
So replace the line with
am.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pi);
}
Bas van Stein is correct. AlarmManager uses an absolute time: milliseconds since epoch, January 1, 1970. You are attempting to set it to a relative time: milliseconds from the time you are setting the alarm. AlarmManager interprets that as being some time very close to epoch, around January 1, 1970, at 12:10AM. Since that time is long since past, the alarm triggers immediately.
The solution is simply to use time.getTimeInMillis() in set instead of trigger.
Some other notes for doing this kind of work, since your app is similar in intention to mine:
Most alarms since API 19 will be inexact, as Frank N. Stein pointed out. The degree of this inexactness depends on implementation and also how far in the future the alarm is created. For something like medicine which is probably at least daily, in all of my testing the alarm has fired within a minute of the intended time.
You are using the current time as the requestCode in your PendingIntent. By setting this to a number that is meaningful to your program and storing that value, you can later update or cancel this alarm by recreating the same PendingIntent.
From API level 23 and forward, urgent alarms like medication alarms should use setAndAllowWhileIdle() to ensure that their alarm goes off during low power idle states. I do not yet know if using set with RTC_WAKEUP will be sufficient to ensure this behavior.
From the official docs:
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.

Why alarmmanager not exactly repeats an event android

Please help me. Why does AlarmManager not exactly repeats an event when the repetition period of more than one day?
Here is my code to run AlarmManager. The variable time_period contains the following value 60*1000*60*24*7 which is equal to 7 days. In the end, if I change the date on your phone, the event is triggered by 6-4-7-7-7-7 days.
This can be seen in the logs of the application (See link http://prntscr.com/7kdqbw ) Thanks in advance for your reply.
Intent notification = new Intent(this , ServiceReminders.class);
notification.putExtra("backup", "backup");
AlarmManager alarmManagerBackup = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pibackup = PendingIntent.getService(this, 3, notification, 10);
if (sdkVersion < 19) {
alarmManagerBackup.setRepeating(AlarmManager.RTC_WAKEUP, 0, time_period, pibackup);
}
else if (sdkVersion >= 19) {
alarmManagerBackup.setInexactRepeating(AlarmManager.RTC_WAKEUP, 0, time_period, pibackup);
}
Log.d("ServiceManagerNotification", "AlarmBackup sdkVersion = "+sdkVersion);
You are explicitly leveraging the inexact alarms by calling setInexactRepeating(). Unsurprisingly, this results in your alarms being set at inexact intervals.
The problem is further compounded by the fact that you are supplying a custom period. If you do not use one of the pre-defined intervals, then the framework will simply call setRepeating() using your interval instead of using setInexactRepeating().
From the documentation for the intervalMillis parameter:
interval in milliseconds between subsequent repeats of the alarm. Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY then the alarm will be phase-aligned with other alarms to reduce the number of wakeups. Otherwise, the alarm will be set as though the application had called setRepeating(int, long, long, PendingIntent). As of API 19, all repeating alarms will be inexact and subject to batching with other alarms regardless of their stated repeat interval.
In the end, regardless of how you set this alarm, it will be inexact if you set it as a repeating alarm and there isn't a whole lot you can do about it.
If you need more precision, you should use either setWindow() or setExact() and set the next alarm every time your alarm triggers. Although if you are only performing an action once a week, it is likely that in the end you don't need that precision.

AlarmManager.RTC_WAKEUP not working for some devices

I am using AlarmManager in my application to set alarm for a particular time. I have used AlarmManager.RTC_WAKEUP to set the alarm. When I am testing the same it's working on number of device like Lg optimus, Sony Xperia etc. But while testing the same app in Samsung Galaxy S3 I found that alarm is not working. I am still unable to understand why is this happening.
I am using following code to set alarm :-
// create the object
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,cal1.getTimeInMillis(), PendingIntent.getBroadcast(getActivity(),reminderId, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Someone please help me to solve this strange problem. Any help would be appreciable.
Thank you
I'm not sure but it sounds the problem is with the way you set cal1's time and maybe different timezone.
I'm not sure what the particular issue you are having is, but you could try to replace cal1.getTimeinMillis() with System.currentTimeMillis() + your time delta in ms. This works consistently for me.
Is your testing getting tripped up by the long-time Android OS bug https://code.google.com/p/android/issues/detail?id=2880 ? If the clock gets set backwards, Intent.ACTION_DATE_CHANGED broadcast won't be delivered until the clock catches up to what was going to be the alarm time. (Reboot to reset this broken state.) That's because this Intent is based on an AlarmManager alarm which does not get updated when the clock changes.
That situation is pretty rare in normal use. But it occurs frequently when testing alarm code.
If you're testing an AlarmManager alarm by adjusting the clock, AFAIK any alarm set to a target Calendar on any Android device will get messed up by adjusting the clock. You can work around it by rescheduling your alarm whenever the clock gets adjusted. This assumes the system properly broadcasts Intent.ACTION_TIME_CHANGED so you can tell when the clock gets adjusted. (That Intent is misnamed. It's not like Intent.ACTION_DATE_CHANGED which occurs when the date changes due to time passing.)
I think you have problem in your "cal1" object;
When you set time in calendar object, actually setting Month, you should not use exact numeric value of month;
ex: - April = 4, you must use 3 instead of 4
I face this problem in API Level 19, but don't know about others.
This Question may give you better explanation.Why month give wrong value
public Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,2014);
c.set(Calendar.MONTH, 03);//One minus from actual month numeric value
c.set(Calendar.DATE, 24);
c.set(Calendar.HOUR_OF_DAY,16 );
c.set(Calendar.MINUTE, 39);
c.set(Calendar.SECOND, 0);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pi);

Categories

Resources