Saphire Saphire - 1 year ago 41
Android Question

AlarmManager triggers way too soon

I let my background service send the geo data of the device to an API.

private static long LOCATION_INTERVAL = 1800000;

Is supposed to be the interval for the location service and the

The first alarm I fire in
like this

Intent i = new Intent(this, typeof(LocationService));
PendingIntent pending = PendingIntent.GetService(this, 1, i,

AlarmManager alarm = (AlarmManager)GetSystemService(AlarmService);
alarm.SetExact(AlarmType.RtcWakeup, 30000, pending);

Then, in the Service itself, I re-trigger the alarm all the time like this

Intent intent = new Intent(this, typeof(LocationService));
PendingIntent pending = PendingIntent.GetService(this, 100, intent,
AlarmManager alarm = (AlarmManager)GetSystemService(AlarmService);

Problem: The service gets called way too soon (+/- every minute!).

Question: How can I make my alarm manager stick to

Answer Source

Could you please check the solution below and let me know the results?

If does not work, I delete the answer....


I believe the error is here:

alarm.SetExact(int type, long triggerAtMillis, PendingIntent operation);

triggerAtMillis: time in milliseconds that the alarm should go off, using the appropriate clock (depending on the alarm type).

So, your are using 1800000 as triggerAtMillis. However, 1800000 is following date in UTC: Thu Jan 01 1970 00:30:00

Since this is an old date, the alarm is fired immediately.


Maybe, you should update your code as follows:

In MainActivity, I believe that you want to fire the alarm immediately. So, create it as follows:

alarm.SetExact(AlarmType.RtcWakeup, Calendar.getInstance().getTimeInMillis(), pending);

In your service, it seems that you want to trigger your alarm after 1800000. So, you have to use:

alarm.SetExact(AlarmType.RtcWakeup, Calendar.getInstance().getTimeInMillis() + LOCATION_INTERVAL, pending);

This way, alarm will be fired 30 minutes after current time (current time + LOCATION_INTERVAL).

Keep in mind that second parameter is the date in milliseconds... It is a number which represents an whole date (and not only an interval)...