0

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!

1 Answers1

1

Why not do this to get the time.

Calendar cal = Calendar.getInstance();

int now = cal.getTimeInMillis();

cal.add(Calendar.DAY_OF_MONTH, 1);

int tomorrow = cal.getTimeInMillis();

I think the calender could be what's doing it.

PS. I was the person who suggested the alarm in the last post :P Looks like you are doing good.


That useful code:

//Create a calendar.
Calendar cal = Calendar.getInstance();

//Get the time in miilis since Jan 1st 1970 I think? 
cal.getTimeInMillis();

//Set the calendar
cal.set(year, month, day, hour, minute, second);

//set a certain aspect of the calendar
cal.set(Calendar.<What you want to set>, <amount>);

//for example
cal.set(Calendar.DAY_OF_MONTH, 1);

//Add to the calendar, Calendar.DAY_OF_MONTH what ever you are using specifies the amount of milliseconds to add, for example a day is 100*60*60*24 milliseconds where a second is 1000 milliseconds.
cal.add(Calendar.<Day or month or what ever>, <amount>);

//for example
cal.add(Calendar.MONTH, 1);

//Take of the calendar
cal.add(Calendar.MONTH, -1);

//Get that value
int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);

Hope you enjoy :)

FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • Wow, I never really tought of it that way...will calendar automaticlly edit month or year if the day + 1 is in the next month or year? – Tomáš 'Guns Blazing' Frček May 05 '12 at 09:14
  • Well if you add one day (1000*60*60*24 miliseconds) to the total milliseconds that is already there when you go calendar.getTimeInMillis() it just gets the total milli seconds, so what I am saying is that it just adds the milliseconds in a day, so in short, yes it will go over to the next month, ill add some more useful code to my answer for you – FabianCook May 05 '12 at 09:21
  • One last thing, when Im adding a day to calendar instance, does it realy matter if I add day_of_month, day_of_week, or day_of_year? – Tomáš 'Guns Blazing' Frček May 05 '12 at 11:49
  • It doesn't matter which one you use when adding it because they are all the same thing when it comes to time, it only matters when your getting or setting – FabianCook May 05 '12 at 21:22