0

i am having this code for setting multiple alarms for creating a reminder application .The code works fine when a single alarm is put. However when i set mutiple alarms I am getting only the first reminder and that too invoked when the alarm time of the last reminder is reached.

          import java.util.Calendar;

          import android.app.AlarmManager;
          import android.app.PendingIntent;
          import android.content.Context;
          import android.content.Intent;

public class ReminderManager {

private Context mContext; 
private AlarmManager mAlarmManager;

public ReminderManager(Context context) {
    mContext = context; 
    mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}

public void setReminder(Long taskId, Calendar when) {

    Intent i = new Intent(mContext, OnAlarmReceiver.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId); 

    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 

    mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}

}

On alarm reciever i have

public class OnAlarmReceiver extends BroadcastReceiver {

private static final String TAG = ComponentInfo.class.getCanonicalName(); 


@Override   
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "Received wake up from alarm manager.");

    long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);

    WakeReminderIntentService.acquireStaticLock(context);

    Intent i = new Intent(context, ReminderService.class); 
    i.putExtra(RemindersDbAdapter.KEY_ROWID, rowid);  
    context.startService(i);

}

}

Ashish Augustine
  • 1,784
  • 4
  • 20
  • 50
  • Check the below post: http://stackoverflow.com/questions/5549974/android-set-multiple-alarms-simultaneosuly – Maneesh Nov 07 '11 at 12:21

1 Answers1

0

Your code shows that you have hard coded the 2nd parameter of

PendingIntent.getBroadcast(c, 0, i, etc

As far as I am aware, this parameter is the identifier for your alarm. Use a different value for each alarm if you want to set multiple alarms.

Try

PendingIntent.getBroadcast(c, taskid, i, etc

Alternatively, I handled this by only ever setting one alarm at a time (the earliest one).

When that alarm is triggered I then set the alarm for the next one.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • How do you set the alarm for next time. can it be done automatically.? Or you set alarm after every wake up is recieved...Can it be done programatically – Ashish Augustine Nov 07 '11 at 13:10
  • I have a function called SetupNextAlarm which handles that. I just call it whenever I need to set up the next alarm whether that is the first time I am setting it up or as a result of a previous alarm being fired. The code that runs when my alarm is fired simply calls my SetupNextalarm function before it exits. – Kuffs Nov 07 '11 at 13:25
  • so does that mean u can set multiple alarm with that...can u plz post that comment – Ashish Augustine Nov 07 '11 at 14:09