Im trying to make an app, that would remind me of an event at the same time every day. For this purpouse Im using an AlarmManager.
This is how I register an event:
Intent alarmIntent = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntent.putExtra("id_bundle", cursor.getInt(0));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), cursor.getInt(0), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
This is how I unregister an event:
Intent alarmIntent = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntent.putExtra("id_bundle", id);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), id, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pendingIntent);
This is how I edit an event:
Intent alarmIntent = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntent.putExtra("id_bundle", id);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), id, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pendingIntent);
Intent alarmIntentNew = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntentNew.putExtra("id_bundle", id);
PendingIntent pendingIntentNew = PendingIntent.getActivity(getApplicationContext(), id, alarmIntentNew, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntentNew);
I tried using setRepeating with an interval 24*3600*1000 but that didnt seem to work, so I decided to unregister the event when it triggers and register it again. This I do in the ReminderAlarm activity that starts after the alarm triggers.
Also, whenever I start the application, I register all the events again using the block of code I posted to register an event. I pull data about what events and what times to register them from a database.
My problem is that this doesent work either. When I add an event to a time T+1 minute, it triggers correctly. But the next day, it doesent.
Could it be perhaps because I give the calendar instance wrong data? I have quite a long function that sets up the calendar instance after an event triggers and before I send this calendar instance into the AlarmManager instance. In this function I chech for things like new month, new year etc and set calendar fields (year, month, day_of_month, hour_of_day, minute). Is this even necesary? Does calendar.getTimeInMillis() consider all those fields or just hour_of_day and minute? Do you see what might be wrong? Thank you!