1

I have built an alarm which works properly:

public static void setAlarm(int hours, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hours);
        calendar.set(Calendar.MINUTE, minutes);
        Intent myIntent = new Intent(PreferenceHelper.getContext(), TimeManagementAdapter.AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    }

Now, how can I edit the alarm time and delete the alarm?

Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77

1 Answers1

1

For cancelling the alarm you may call AlarmManager.cancel with your PendingIntent.

Intent myIntent = new Intent(PreferenceHelper.getContext(), TimeManagementAdapter.AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0);
alarmManager.cancel(pendingIntent);

For editing the alarm, you have the option to first cancel it and re-add it or you may add PendingIntent.FLAG_UPDATE_CURRENT as the last option when scheduling a new alarm.

PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0, PendingIntent.FLAG_UPDATE_CURRENT);

Possible duplicate Cancel an AlarmManager pendingIntent in another pendingintent

Community
  • 1
  • 1