0

I'm trying to make an app that allows the user to set tasks and alarms or notifications for each task. I have created a 'setAlarm' method below. However, I have an error that whenever I set multiple tasks with alarms, somehow all the previous ones get cancelled and only the most recently set alarm will go off. Do you know whats the problem? My guess is that the 'calendar' instance gets reset every time I call 'setAlarm'. How could I get around this?

  public void setAlarm() {
      Intent intent1 = new Intent(NewGoal.this, SingleAlarm.class);
      PendingIntent sender = PendingIntent.getBroadcast(NewGoal.this,
              0, intent1, 0);

      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(System.currentTimeMillis());

      if (alarm_time == 10) {
          calendar.add(Calendar.SECOND, alarm_time);
      } else if (alarm_time == 30 {
          calendar.add(Calendar.SECOND, alarm_time)
      }

      AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
      am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
      Log.i(TEST, "In setAlarm method");
      Log.i(TEST, "calendar=" + calendar.MILLISECOND);
}
Andrew
  • 1
  • I don't see anything obvious. In your `AlarmManager` you're passing in a long value from the `calendar` object, so that is presumably what is used to determine when to fire off the alarm at the correct time. I'm guessing then that your problem is actually in the `AlarmManager` somewhere, either in the `set()` method or the method that actually fires off the alarms at the right time. – Stewart Murrie Feb 08 '11 at 02:42
  • I'd recommend changing the [java] tag to [android] to get android developers' attention. – Bert F Feb 08 '11 at 02:50
  • @britishmutt - you are right. It seems when I call the set() method it cancels the previous intent. How can I avoid this problem? – Andrew Feb 08 '11 at 08:26

3 Answers3

1

From the documentation:

If there is already an alarm scheduled for the same IntentSender, it will first be cancelled.

dave.c
  • 10,910
  • 5
  • 39
  • 62
0

May be because you are working with milli second granularity and in Java these is not guaranteed to be accurate. Below is from the JavaDoc. May be you should use System.nanoTime() but this is available only since Java 5. Read this thread on SO: System.currentTimeMillis vs System.nanoTime

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

Also, the below statement from your code is redundant

calendar.setTimeInMillis(System.currentTimeMillis());
Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • Well, I'm not sure why that issue would affect alarms that differ by minutes or hours. Thanks for the heads up about the redundancy. I think somehow the alarmManager is getting reset every time 'setAlarm' is run. – Andrew Feb 08 '11 at 07:32
0

@dave.c Thanks, I found this in the documentation too. Here is how I got around it.

  final int intent_id= (int) System.currentTimeMillis();

  Intent intent1 = new Intent(NewGoal.this, AlarmBroadcastReceiver.class);
  PendingIntent sender = PendingIntent.getBroadcast(NewGoal.this, intent_id, intent1, 0); 

If there is more than one intent with the same name, the most recent one cancels the previous one. To get around this, use the current time to make each intent different.

Thanks guys.

Andrew
  • 1