1

This problem seems a little bit odd, but if someone as encounter something like this, please help me...

I created an Alarm Scheduler, that sends an alarm to the user using AlarmManager, through this code:

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra("tk_alert_id", lastAlertId.getId()+"");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, idRandom, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);

The problem is that, sometimes, I receive this alarm on my AlarmReceiver (BroadcastReceiver) at a wrong time, as you can see in the image bellow: image, and I can't figure out what's the problem... I checked the time for date and was set as "2015-05-27 17:00:00", but it was received a little minutes earlier (around 16:57) ...

Does anyone knows what kind of problem I am encountering here?

slyzer
  • 215
  • 1
  • 19

1 Answers1

2

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

Community
  • 1
  • 1
Zain
  • 2,336
  • 1
  • 16
  • 25
  • 1
    Hi, thank you for your comment... I don't want to set a repeating alarm... just an one time only alarm... This problem was seen in a Android with a API 18... is there a difference for the `alarmManager.set` like for the `alarmManager.setRepating`? – slyzer Jun 01 '15 at 14:08
  • "set" is a one-off alram and setRepeating repeats. For your purpose (one-off, exact alarm) use alarmManager.setExact(....) - here are the docs - http://developer.android.com/reference/android/app/AlarmManager.html#setExact(int, long, android.app.PendingIntent) – Zain Jun 01 '15 at 14:37
  • It seems that the problem didn't occur again, so I suppose this issue is solved... I'm gonna accept this answer – slyzer Jul 15 '15 at 10:00