0

I am facing this really weird problem. I have made a working alarm system. But when I delete the alarm, it calls it.

AlarmBuddy.java (Working class to set original alarm)

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, m-1);
calendar.set(Calendar.DAY_OF_MONTH, d);
calendar.set(Calendar.HOUR, 8);
calendar.set(Calendar.MINUTE, repeat%60);
final Intent myIntent = new Intent(this.context, AlarmMiddle.class);
myIntent.putExtra("name", name);
myIntent.putExtra("id", repeat);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

super.onBackPressed();

PendingIntent pending_intent = PendingIntent.getBroadcast(AlarmBuddy.this, repeat, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);

ListRecord.java THE FLAW

public Boolean cancelAlarm(int id){
    Intent myIntent = new Intent();
    PendingIntent.getBroadcast(context, id, myIntent,
            PendingIntent.FLAG_CANCEL_CURRENT).cancel();
    return false;
}

This function is to cancel the alarm. This has some problem to say this again:

The code can SET AND CALL an alarm. But when I delete it from a DIFFERENT class, it immediately calls the alarm. Maybe I am just a noob. Or maybe this is an unsolvable problem.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31

2 Answers2

2

This is working for me at all in my application see link

 Intent intent = new Intent(getBaseContext(), Your_Service.class);
 PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), reqcode, intent, 0);
 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
 alarmManager.cancel(pendingIntent);
Shaishav
  • 5,282
  • 2
  • 22
  • 41
NIKHIL SONI
  • 84
  • 1
  • 1
2

To cancel the alarm, you need to use the same PendingIntent as you used to set it. You should also tell the AlarmManager to cancel the alarm as well as canceling the PendingIntent. Try this:

public Boolean cancelAlarm(int id) {
    Intent myIntent = new Intent(context, AlarmMiddle.class);
    PendingIntent pending_intent = PendingIntent.getBroadcast(context, id, myIntent,
                     PendingIntent.FLAG_NO_CREATE);
    if (pending_intent != null) {
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pending_intent);
        pending_intent.cancel();
    }
    return false;
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274