2

While creating an alarm application with multiple alarms Am using PendingIntent with different request codes and an AlarmService to display dialog .During this even before first dialog is snoozed or dismissed the second dialog pops up.How can I prevent this.

 public void setAlarm() {
        AlarmManager amgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);       
        PendingIntent pi = PendingIntent.getService(context, requestcode, i, PendingIntent.FLAG_CANCEL_CURRENT);
         min = (amod.getHour() * 60) + amod.getMinute();
         amgr.set(amgr.RTC,System.currentTimeMillis()+(min*60*1000), pi);

}

public class AlarmService extends Service {

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    playTone();
    showDialog();

    return START_NOT_STICKY;
}
Aravindraj
  • 590
  • 5
  • 23

1 Answers1

0

I think you have two options to handle this:

1) Make sure that different request will not occur at the same time. This can be done by holding a list of all the request times and if a new one collides with an existing one, just delay it by X seconds (with this implementation, you are not guaranteed that there are no collisions, but their chance is reduced).

2) Postpone the display of a new dialog until the previous dialog is dismissed. To implement this, your dialog and service need to communicate. The dialog needs to be notified when a new message is pending so it can be displayed when the old dialog is dismissed & the service needs to be notified when the dialog is cleared so it would know that if a new message is requested, it can show.

Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60