vetalitet
vetalitet

Reputation: 703

Notification shows not in exact time

I know that this topic is explained fairly well and there are a lot of tutorials. But maybe I'm too new in android to understand what I'm doing wrong.

I need to implement support of set of reminders. And notification should be shown exactly every Monday at 15 pm. I checked a lot of tutorials and similar questions on this site but anyway notification shows, somehow, randomly.

How do I test implementation:

  1. Current time is 14:55
  2. I set reminder on Monday at 15:00
  3. And right after SAVE notification is shown.
  4. Because of (just for example) I set repeat period in 20 sec, this notification is shown again and again with delay of 20 sec. But current time is still between 14:55 and 15:00.

And my task is to run notification at 15:00 or a liitle later. But not before.

set repeating notification

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, NotifyService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, (int) reminder.id, intent, 0);

final Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK, 2);
calendar.set(Calendar.HOUR_OF_DAY, reminder.time.hour);
calendar.set(Calendar.MINUTE, reminder.time.minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,
        Calendar.getInstance().getTimeInMillis(), 20000, pendingIntent);

BTW

my min SDK version is 7, so I cannot use methods like setExact()

android version on tested device is 4.4.2

Thanks

Upvotes: 1

Views: 962

Answers (1)

nitzanj
nitzanj

Reputation: 1699

When you set the alarm you set it with Calendar.getInstance().getTimeInMillis() instead of calendar.getTimeInMillis(). Simple bug ;).

Basically setting the alarm to 'now' every time, ignoring your calendar object.

Upvotes: 1

Related Questions