7

I am working on an Alarm Clock project and i want to edit my already set Alarm. when I edit alarm then alarm time is updated but values that I send by using putExtra() are not changing. I am using PendingIntent.FLAG_ONE_SHOT flag.

But when I set flag PendingIntent.FLAG_UPDATE_CURRENT all putExtra() values are also change but now problem is that, when i click on stop button and finish() the current activity it calls again.

means when I go to finish the activity it calls again on button click while I am finishing current activity. please help me. Thanks in advance.

Sunit Kumar Gupta
  • 1,203
  • 1
  • 11
  • 19

2 Answers2

7

My prefered way to update a PendingIntent in AlarmManager is to Cancel it and re-set it
do not forget to cancel :

1) AlarmManager.cancel(pendingIntent) with a pendingIntent that match your pending intent (same class , same action ... but do not care about extra see IntentFilter)
2) pendingIntent.cancel();
3) pendingIntent = new PendingIntent() ... and do other settings
4) AlarmManager.set(... to provide new PendingIntent

Emmanuel Devaux
  • 3,327
  • 4
  • 25
  • 30
  • In case of calendar events setting alarm, If we use a local variable and not static, we can assign various time for various events. But it seems that you have shown static pendingIntent example – Pattabi Raman Sep 23 '11 at 13:31
  • Is REALLY this is the only way to update the alarm? WORKED FOR ME. – Shajeel Afzal Aug 22 '13 at 08:58
2

Each alarm has its unique identifier, if you want to update an alarm, you can create a new one with the same UNIQUE_ID.

PendingIntent pi = PendingIntent.getBroadcast(this, PENDING_INTENT_ID, intent, 0);

Check this answer

Community
  • 1
  • 1
Nissar
  • 2,360
  • 2
  • 13
  • 12