How to cancel alarm in android?

Preeyah picture Preeyah · Sep 3, 2012 · Viewed 7.1k times · Source

I'm working on an app where I create events and set alarm for the events. I need to know how to cancel alarm. I'm having trouble with it. Anyone can help me please, thanks.

UPDATE
Posted the code, see the code below. AlarmClass.java

Button buttonCancel = (Button) findViewById(R.id.btnCancel);
        buttonCancel.setOnClickListener(new Button.OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {
                Intent i = new Intent(AlarmClass.this, AlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmClass.this, 1, i, 0);



                AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                aManager.cancel(pendingIntent);

                Toast.makeText(getApplicationContext(), "Alarm has been cancelled", Toast.LENGTH_SHORT).show();
            }


        });

Answer

Lucifer picture Lucifer · Sep 3, 2012

You can simply cancel an Alarm By cancel() method of AlarmManager.

AlarmManager am; 
.
.
.
am.cancel ( PendingIntent operation ); 

for e.g.

Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this,0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.cancel(sender);

You need to pass the PendingIntent's object into cancel() method.