AlarmManager starts PendingIntent too soon

I searched for 3 days but did not find a solution or a similar problem / question elsewhere. Here is the deal:

1 hour trigger β†’ working correctly

Trigger after 2 hours β†’ Go to 1:23

1 day trigger β†’ Go to ~ 11: 00

So why is AlarmManager so unpredictable and always too soon? Or what am I doing wrong? And is there any other way so that it can work correctly?

So I register my PendingIntent in the AlarmManager (stripped down):

AlarmManager alarmManager = (AlarmManager)parent.getSystemService(ALARM_SERVICE); Intent myIntent = new Intent(parent, UpdateKlasRoostersService.class); PendingIntent pendingIntent = PendingIntent.getService(parent, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); //Set startdate of PendingIntent so it triggers in 10 minutes Calendar start = Calendar.getInstance(); start.setTimeInMillis(SystemClock.elapsedRealtime()); start.add(Calendar.MINUTE, 10); //Set interval of PendingIntent so it triggers every day Integer interval = 1*24*60*60*1000; //Cancel any similar instances of this PendingIntent if already scheduled alarmManager.cancel(pendingIntent); //Schedule PendingIntent alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, start.getTimeInMillis(), interval, pendingIntent); //Old way I used to schedule a PendingIntent, didn't seem to work either //alarmManager.set(AlarmManager.RTC_WAKEUP, start.getTimeInMillis(), pendingIntent); 

It would be great if someone had a solution. Thanks for any help!

Update: 2 hours ago, he worked to start it with an interval of 2 hours, but after that it worked after 1:20 hours. This is getting really weird. I will track the triggers with a log file and post it here tomorrow.

Update: It is expected that PendingIntent will run every 3 hours. From the second line of the log, it seems that the old scheduled PendingIntent is still running:

 [2012-5-3 2:15:42 519] Updating Klasroosters [2012-5-3 4:15:15 562] Updating Klasroosters [2012-5-3 5:15:42 749] Updating Klasroosters [2012-5-3 8:15:42 754] Updating Klasroosters [2012-5-3 11:15:42 522] Updating Klasroosters 

But I'm sure that I canceled the scheduled PendingIntent before planning a new one. And each PendingIntent is not recreated in the same way, so it should be exactly the same. If not, this thread issue is no longer relevant.

+6
source share
4 answers

When using a calendar, you take into account that the calendar uses time up to milliseconds. Maybe you should set the second Milli field and the seconds field to zero so that it continues on the point.

Also during the day it would be easier to use this

 Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(0); cal.add(Calendar.DAY_OF_MONTH, 1); 

Also, when you use getInstance, aren't the calendars set at the time it was created, so it shouldn't be necessary to set the time again?

+1
source

Rewrite . In the end, I saw your mistake, but unpredictable.

I changed this:

 PendingIntent.getService(parent, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

:

 PendingIntent.getService(parent, 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

under the same assumption that you are broadcasted by some old intention. I have not seen this accident since ...

And only once, when I saw it, it was during my initial call. Another approach would be to track the current and previous Calendar objects if this interval is not as expected and then ignore this β€œearly” broadcast. (Although this method seems redundant, given how the alarm works, it helps prevent these extraneous calls, given how the alarm works).

Hope this helps, I will let you know if I find anything else.

+1
source

I know this question is a bit outdated, but I had the same problem. I found out that if I try to declare a calendar variable outside the method, it will not play well, and alarms will be triggered earlier. Since your class is being cleaned, it's hard to pinpoint exactly where you are invoking the calendar instance.

If I set him up as such, he will shoot right on time:

 protected void nextAlarm(Context context, int seconds){ Calendar nextAlarm = Calendar.getInstance(); Intent intent = new Intent(context, MyClass.class); PendingIntent pending = PendingIntent.getBroadcast(context, MainActivity.REPEATING_ALARM, intent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager amanager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); nextAlarm.add(Calendar.SECOND, seconds); amanager.set(AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), pending); } 
+1
source

Make sure your onStartCommand service returns START_NOT_STICKY, otherwise it will be automatically retried:

 public class UpdateKlasRoostersService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { buildUpdate(); return START_NOT_STICKY; } } 
+1
source

Source: https://habr.com/ru/post/917261/


All Articles