1

I am trying to use this to set an alarm that goes off everyday.

String alarm = Context.ALARM_SERVICE;
                Calendar calendar = Calendar.getInstance();
                AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm);

                    Intent intent = new Intent("NEW_ITEM");
                    PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
                     calendar.setTimeInMillis(System.currentTimeMillis());
                     am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1*AlarmManager.INTERVAL_DAY, sender);
yoshi24
  • 3,147
  • 8
  • 45
  • 62

2 Answers2

1

Without running it, the code looks good to me... Obviously, if you set this alarm every time you start the activity, the alarm will go off immediately since: am.setRepeating(AlarmManager.RTC_WAKEUP, **calendar.getTimeInMillis()**, 1*AlarmManager.INTERVAL_DAY, sender); Tells the alarm manager to alert right now (2nd param) and to repeat in a day (3rd param, assuming your constant is correct).

If you want the alert to start only in 24 hours, simply change the line to:

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY, sender);
IncrediApp
  • 10,303
  • 2
  • 33
  • 24
  • Okay thats cool. I just want to make sure the alarm is being set so if the app is opened in a day the alerts still come. – yoshi24 Aug 29 '11 at 14:34
0

Code looks good, but you have to be aware of one thing. If the user decides to set the alarm again (for example, by hitting the 'set the alarm' button) the old one will be replaced. If you want to avoid this, check out this topic: Using Alarmmanager to start a service at specific time

Community
  • 1
  • 1
Moyshe
  • 1,122
  • 1
  • 11
  • 19